aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclarkzjw <[email protected]>2023-02-21 17:39:59 -0800
committerclarkzjw <[email protected]>2023-02-21 17:39:59 -0800
commit5b09e9d190b642a4e5571c0e4504c155f56a6f5a (patch)
treee4a1ea917b1f5e56d34394ac4d62bb0e71571bf3
parent431e8abcc4bf858e4ca945384a2f60702f1b5a4c (diff)
downloadswarm2fediverse-5b09e9d190b642a4e5571c0e4504c155f56a6f5a.tar.gz
bot: enable location search
-rw-r--r--bot.py (renamed from bot/bot.py)65
-rw-r--r--bot/__init__.py13
-rw-r--r--foursquare/poi.py14
3 files changed, 58 insertions, 34 deletions
diff --git a/bot/bot.py b/bot.py
index 2710184..efde56f 100644
--- a/bot/bot.py
+++ b/bot.py
@@ -5,9 +5,23 @@
5import logging 5import logging
6import io 6import io
7import telegram.constants 7import telegram.constants
8from telegram import __version__ as TG_VER
9
10try:
11 from telegram import __version_info__
12except ImportError:
13 __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
14
15if __version_info__ < (20, 0, 0, "alpha", 1):
16 raise RuntimeError(
17 f"This example is not compatible with your current PTB version {TG_VER}. To view the "
18 f"{TG_VER} version of this example, "
19 f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
20 )
21
8from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, ReplyKeyboardMarkup, KeyboardButton 22from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, ReplyKeyboardMarkup, KeyboardButton
9from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters, ConversationHandler, CallbackContext 23from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters, ConversationHandler, CallbackContext
10from ..config import BOT_TOKEN 24from config import BOT_TOKEN
11from foursquare.poi import query_poi 25from foursquare.poi import query_poi
12from dbstore.dbm_store import get_loc 26from dbstore.dbm_store import get_loc
13from toot import mastodon_client 27from toot import mastodon_client
@@ -16,7 +30,7 @@ from typing import TypedDict, List, cast
16scheduler = None 30scheduler = None
17PRIVACY, TOOT = map(chr, range(8, 10)) 31PRIVACY, TOOT = map(chr, range(8, 10))
18 32
19WAIT_LOC, LOCATION, PHOTO, PROCESS_PHOTO, FINAL, SETTING = range(6) 33WAIT_LOC, LOCATION, LOCATION_SEARCH, PHOTO, PROCESS_PHOTO, FINAL, SETTING = range(7)
20 34
21# Enable logging 35# Enable logging
22logging.basicConfig( 36logging.basicConfig(
@@ -39,7 +53,7 @@ SETTING_MENU = ReplyKeyboardMarkup(
39) 53)
40 54
41 55
42async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 56async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
43 hello = "Hello, this is `checkin.bot`. \n\n" \ 57 hello = "Hello, this is `checkin.bot`. \n\n" \
44 "This is a Telegram bot with functionality similar to Foursquare Swarm, " \ 58 "This is a Telegram bot with functionality similar to Foursquare Swarm, " \
45 "but check in and post your location to the Fediverse (Mastodon/Pleroma) instead of Twitter.\n\n" \ 59 "but check in and post your location to the Fediverse (Mastodon/Pleroma) instead of Twitter.\n\n" \
@@ -53,21 +67,15 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
53 return LOCATION 67 return LOCATION
54 68
55 69
56async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 70async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
57 keyboard = [] 71 context.user_data["latitude"] = update.message.location.latitude
72 context.user_data["longitude"] = update.message.location.longitude
73 await update.message.reply_text("You can input location search keywords or press skip", reply_markup=SKIP_MENU)
58 74
59 for poi in query_poi(update.message.location.latitude, update.message.location.longitude): 75 return LOCATION_SEARCH
60 keyboard.append([
61 InlineKeyboardButton(poi["name"], callback_data=poi["fsq_id"]),
62 ])
63 76
64 reply_markup = InlineKeyboardMarkup(keyboard)
65 await update.message.reply_text("Where are you?", reply_markup=reply_markup)
66 77
67 return WAIT_LOC 78async def process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
68
69
70async def process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
71 query = update.callback_query 79 query = update.callback_query
72 await query.answer() 80 await query.answer()
73 print(query.data) 81 print(query.data)
@@ -97,6 +105,29 @@ async def process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -
97 return PHOTO 105 return PHOTO
98 106
99 107
108async def location_search_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
109 location_search = update.effective_message.text
110 latitude = context.user_data["latitude"]
111 longitude = context.user_data["longitude"]
112
113 keyboard = []
114
115 for poi in query_poi(location_search, latitude, longitude):
116 keyboard.append([
117 InlineKeyboardButton(poi["name"], callback_data=poi["fsq_id"]),
118 ])
119
120 reply_markup = InlineKeyboardMarkup(keyboard)
121 context.user_data["location_search"] = location_search
122 await update.message.reply_text("Where are you? ", reply_markup=reply_markup)
123
124 return WAIT_LOC
125
126
127async def skip_location_search(update: Update, context: ContextTypes.DEFAULT_TYPE):
128 return WAIT_LOC
129
130
100async def tos(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 131async def tos(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
101 await update.message.reply_text("TOS", reply_markup=MAIN_MENU) 132 await update.message.reply_text("TOS", reply_markup=MAIN_MENU)
102 133
@@ -268,6 +299,10 @@ def main() -> None:
268 MessageHandler(filters.LOCATION, checkin), 299 MessageHandler(filters.LOCATION, checkin),
269 ], 300 ],
270 WAIT_LOC: [CallbackQueryHandler(process_callback)], 301 WAIT_LOC: [CallbackQueryHandler(process_callback)],
302 LOCATION_SEARCH: [
303 MessageHandler(filters.TEXT, location_search_callback),
304 CommandHandler("skip", skip_location_search),
305 ],
271 PHOTO: [MessageHandler(filters.PHOTO, photo), 306 PHOTO: [MessageHandler(filters.PHOTO, photo),
272 CommandHandler("skip", skip_photo)], 307 CommandHandler("skip", skip_photo)],
273 }, 308 },
diff --git a/bot/__init__.py b/bot/__init__.py
deleted file mode 100644
index 5e3c341..0000000
--- a/bot/__init__.py
+++ /dev/null
@@ -1,13 +0,0 @@
1from telegram import __version__ as TG_VER
2
3try:
4 from telegram import __version_info__
5except ImportError:
6 __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
7
8if __version_info__ < (20, 0, 0, "alpha", 1):
9 raise RuntimeError(
10 f"This example is not compatible with your current PTB version {TG_VER}. To view the "
11 f"{TG_VER} version of this example, "
12 f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
13 )
diff --git a/foursquare/poi.py b/foursquare/poi.py
index 722dbb7..5e67d1c 100644
--- a/foursquare/poi.py
+++ b/foursquare/poi.py
@@ -5,7 +5,8 @@ import requests
5from config import FSQ_API_KEY 5from config import FSQ_API_KEY
6from dbstore.dbm_store import store_loc 6from dbstore.dbm_store import store_loc
7 7
8POI_API_ENDPOINT = "https://api.foursquare.com/v3/places/nearby?ll={}%2C{}" 8POI_API_ENDPOINT = "https://api.foursquare.com/v3/places/nearby?ll={}%2C{}&limit=10"
9POI_SEARCH_API_ENDPOINT = "https://api.foursquare.com/v3/places/search?query={}&ll={}%2C{}&radius=2000&limit=10"
9POI_PHOTO_ENDPOINT = "https://api.foursquare.com/v3/places/{}/photos?sort=POPULAR&limit=10" 10POI_PHOTO_ENDPOINT = "https://api.foursquare.com/v3/places/{}/photos?sort=POPULAR&limit=10"
10OSM_ENDPOINT = "https://www.openstreetmap.org/?mlat={}&mlon={}&zoom=15&layers=M" 11OSM_ENDPOINT = "https://www.openstreetmap.org/?mlat={}&mlon={}&zoom=15&layers=M"
11headers = { 12headers = {
@@ -14,24 +15,25 @@ headers = {
14} 15}
15 16
16 17
17def query_poi(latitude, longitude): 18def query_poi(search, latitude, longitude):
18 locations = list() 19 locations = list()
19 20
20 url = POI_API_ENDPOINT.format(latitude, longitude) 21 url = POI_SEARCH_API_ENDPOINT.format(search, latitude, longitude)
21 22 print(url)
22 response = requests.get(url, headers=headers) 23 response = requests.get(url, headers=headers)
23 24
24 for poi in json.loads(response.text)["results"]: 25 for poi in json.loads(response.text)["results"]:
25 loc = { 26 loc = {
26 "fsq_id": poi["fsq_id"], 27 "fsq_id": poi["fsq_id"],
27 "name": poi["name"], 28 "name": poi["name"],
28 "locality": poi["location"]["locality"], 29 "locality": poi["location"]["locality"] if "locality" in poi["location"] else "",
29 "region": poi["location"]["region"], 30 "region": poi["location"]["region"] if "region" in poi["location"] else "",
30 "latitude": poi["geocodes"]["main"]["latitude"], 31 "latitude": poi["geocodes"]["main"]["latitude"],
31 "longitude": poi["geocodes"]["main"]["longitude"], 32 "longitude": poi["geocodes"]["main"]["longitude"],
32 "osm_url": OSM_ENDPOINT.format(poi["geocodes"]["main"]["latitude"], poi["geocodes"]["main"]["longitude"]) 33 "osm_url": OSM_ENDPOINT.format(poi["geocodes"]["main"]["latitude"], poi["geocodes"]["main"]["longitude"])
33 } 34 }
34 locations.append(loc) 35 locations.append(loc)
36 print(loc)
35 store_loc(loc) 37 store_loc(loc)
36 38
37 return locations 39 return locations
Powered by cgit v1.2.3 (git 2.41.0)