Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F628872
setloc.lua
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
setloc.lua
View Options
--[[
mattata v2.0 - Set Location Plugin
Geocodes an address and stores latitude/longitude for weather and time plugins.
]]
local
plugin
=
{}
plugin
.
name
=
'setloc'
plugin
.
category
=
'utility'
plugin
.
description
=
'Set your location for weather and time commands'
plugin
.
commands
=
{
'setloc'
,
'setlocation'
,
'location'
}
plugin
.
help
=
'/setloc <address> - Set your location by providing an address or place name.'
function
plugin
.
on_message
(
api
,
message
,
ctx
)
local
tools
=
require
(
'telegram-bot-lua.tools'
)
local
http
=
require
(
'src.core.http'
)
local
url
=
require
(
'socket.url'
)
local
input
=
message
.
args
if
not
input
or
input
==
''
then
-- Show current location
local
result
=
ctx
.
db
.
call
(
'sp_get_user_location'
,
{
message
.
from
.
id
})
if
result
and
result
[
1
]
then
return
api
.
send_message
(
message
.
chat
.
id
,
string.format
(
'Your location is set to: <b>%s</b>
\n
(<code>%s, %s</code>)'
,
tools
.
escape_html
(
result
[
1
].
address
or
'Unknown'
),
result
[
1
].
latitude
,
result
[
1
].
longitude
),
{
parse_mode
=
'html'
}
)
end
return
api
.
send_message
(
message
.
chat
.
id
,
'You haven
\'
t set a location yet. Use /setloc <address> to set one.'
)
end
-- Geocode via Nominatim
local
encoded
=
url
.
escape
(
input
)
local
api_url
=
string.format
(
'https://nominatim.openstreetmap.org/search?q=%s&format=json&limit=1&addressdetails=1'
,
encoded
)
local
data
,
status
=
http
.
get_json
(
api_url
)
if
not
data
then
return
api
.
send_message
(
message
.
chat
.
id
,
'Failed to geocode that address. Please try again.'
)
end
if
not
data
or
#
data
==
0
then
return
api
.
send_message
(
message
.
chat
.
id
,
'No results found for that address. Please try a different query.'
)
end
local
result
=
data
[
1
]
local
lat
=
tonumber
(
result
.
lat
)
local
lng
=
tonumber
(
result
.
lon
)
local
address
=
result
.
display_name
or
input
-- Upsert into user_locations
ctx
.
db
.
call
(
'sp_upsert_user_location'
,
{
message
.
from
.
id
,
lat
,
lng
,
address
})
return
api
.
send_message
(
message
.
chat
.
id
,
string.format
(
'Location set to: <b>%s</b>
\n
(<code>%s, %s</code>)'
,
tools
.
escape_html
(
address
),
lat
,
lng
),
{
parse_mode
=
'html'
}
)
end
return
plugin
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 15, 5:24 AM (1 d, 22 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
112226
Default Alt Text
setloc.lua (2 KB)
Attached To
Mode
R69 mattata
Attached
Detach File
Event Timeline