aboutsummaryrefslogtreecommitdiff
path: root/bot.py
diff options
context:
space:
mode:
authorclarkzjw <[email protected]>2023-02-21 23:32:21 -0800
committerclarkzjw <[email protected]>2023-02-21 23:32:21 -0800
commit3a76618d0f46c7e28641b74a869544661c504e21 (patch)
tree47af0b7b4dfe9ad5ad6f710c6efe9ab99ab35165 /bot.py
parentefc952972f97f98814b2e9a49648766101cd0725 (diff)
downloadswarm2fediverse-3a76618d0f46c7e28641b74a869544661c504e21.tar.gz
refactoring code
Diffstat (limited to 'bot.py')
-rw-r--r--bot.py329
1 files changed, 4 insertions, 325 deletions
diff --git a/bot.py b/bot.py
index f238e75..f2be049 100644
--- a/bot.py
+++ b/bot.py
@@ -1,342 +1,21 @@
1#!/usr/bin/env python 1#!/usr/bin/env python
2 2
3import telegram.constants
4from telegram import __version__ as TG_VER
5
6try:
7 from telegram import __version_info__
8except ImportError:
9 __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
10
11if __version_info__ < (20, 0, 0, "alpha", 1):
12 raise RuntimeError(
13 f"This example is not compatible with your current PTB version {TG_VER}. To view the "
14 f"{TG_VER} version of this example, "
15 f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
16 )
17
18import logging 3import logging
19from pprint import pprint 4from callback import *
20import io 5from telegram.ext import Application, CallbackQueryHandler, CommandHandler, MessageHandler, filters, ConversationHandler
21from foursquare.poi import OSM_ENDPOINT
22from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, ReplyKeyboardMarkup, KeyboardButton
23from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters, \
24 ConversationHandler, CallbackContext
25from config import BOT_TOKEN, MEDIA_GROUP_TIMEOUT
26from foursquare.poi import query_poi
27from dbstore.dbm_store import get_loc
28from toot import mastodon_client
29from typing import TypedDict, List, cast
30from prompt.string import *
31
32 6
33logging.basicConfig( 7logging.basicConfig(
34 format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO 8 format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
35) 9)
36logger = logging.getLogger(__name__) 10logger = logging.getLogger(__name__)
37 11
38WAIT_LOCATION, LOCATION_SEARCH_KEYWORD, LOCATION_CONFIRMATION, ADD_MEDIA, ADD_COMMENT = range(5)
39
40MAIN_MENU = ReplyKeyboardMarkup([
41 [KeyboardButton(text="Check-in here", request_location=True)],
42])
43
44SKIP_LOCATION_SEARCH = CALLBACK_SKIP
45INLINE_SKIP_MENU = InlineKeyboardMarkup([
46 [telegram.InlineKeyboardButton("Skip", callback_data=SKIP_LOCATION_SEARCH)]
47])
48
49
50class MsgDict(TypedDict):
51 media_id: str
52 caption: str
53 status_id: int
54 content: str
55 chat_id: int
56
57
58async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
59 hello = "Hello, this is `checkin.bot`. \n\n" \
60 "This is a Telegram bot with functionality similar to Foursquare Swarm, " \
61 "but check in and post your location to the Fediverse (Mastodon/Pleroma) instead of Twitter.\n\n" \
62 "Aware of privacy concerns, this bot will not store your location data." \
63 "*Be safe and cautious when sharing your real time location on the web.* \n\n" \
64 "Start using this bot by sharing your location using Telegram context menu to it."
65
66 await update.message.reply_text(hello, parse_mode=telegram.constants.ParseMode.MARKDOWN)
67 await update.message.reply_text(PROMPT_CHOOSE_ACTION, reply_markup=MAIN_MENU)
68
69 return WAIT_LOCATION
70
71
72async def callback_location_sharing(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
73 if update.message.venue is not None:
74 fsq_id = update.message.venue.foursquare_id
75 title = update.message.venue.title
76 context.user_data["fsq_id"] = fsq_id
77 context.user_data["title"] = title
78 context.user_data["latitude"] = update.message.venue.location.latitude
79 context.user_data["longitude"] = update.message.venue.location.longitude
80
81 poi = get_loc(context.user_data["fsq_id"])
82 media_id = []
83 content = f"I'm at {poi['name']} in {poi['locality']}, {poi['region']}, {poi['osm_url']}"
84 status = mastodon_client.status_post(
85 content,
86 visibility="private",
87 media_ids=media_id)
88
89 context.user_data["status_id"] = status["id"]
90 context.user_data["status_content"] = content
91
92 print("status_id", context.user_data["status_id"])
93
94 await update.message.reply_text(
95 text=f"Selected place: {poi['name']}, \nPosted to Mastodon: {status['url']}",
96 parse_mode=telegram.constants.ParseMode.MARKDOWN,
97 )
98
99 prompt_attach_comment_msg = await update.message.reply_text(PROMPT_ADD_COMMENT, reply_markup=INLINE_SKIP_MENU)
100 context.user_data[PROMPT_ADD_COMMENT] = prompt_attach_comment_msg.message_id
101
102 return ADD_COMMENT
103 else:
104 context.user_data["latitude"] = update.message.location.latitude
105 context.user_data["longitude"] = update.message.location.longitude
106
107 await update.message.reply_text("Searching...", reply_markup=telegram.ReplyKeyboardRemove())
108 prompt_msg = await update.message.reply_text(PROMPT_LOCATION_KEYWORD, reply_markup=INLINE_SKIP_MENU)
109
110 context.user_data[PROMPT_LOCATION_KEYWORD] = prompt_msg.message_id
111 return LOCATION_SEARCH_KEYWORD
112
113
114async def callback_manual_location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
115 loc = update.effective_message.text
116 osm_url = OSM_ENDPOINT.format(context.user_data["latitude"], context.user_data["longitude"])
117 media_id = []
118 content = f"I'm at {loc}, {osm_url}"
119 status = mastodon_client.status_post(
120 content,
121 visibility="private",
122 media_ids=media_id)
123
124 context.user_data["status_id"] = status["id"]
125 context.user_data["status_content"] = content
126
127 print("status_id", context.user_data["status_id"])
128
129 await update.message.reply_text(
130 text=f"Manually selected place: {loc}, \nPosted to Mastodon: {status['url']}",
131 parse_mode=telegram.constants.ParseMode.MARKDOWN,
132 )
133
134 prompt_attach_comment_msg = await update.message.reply_text(PROMPT_ADD_COMMENT, reply_markup=INLINE_SKIP_MENU)
135 context.user_data[PROMPT_ADD_COMMENT] = prompt_attach_comment_msg.message_id
136
137 return ADD_COMMENT
138
139
140async def callback_location_confirmation(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
141 query = update.callback_query
142 await query.answer()
143 context.user_data["fsq_id"] = query.data
144
145 await query.delete_message()
146
147 poi = get_loc(context.user_data["fsq_id"])
148 media_id = []
149 content = f"I'm at {poi['name']} in {poi['locality']}, {poi['region']}, {poi['osm_url']}"
150 status = mastodon_client.status_post(
151 content,
152 visibility="private",
153 media_ids=media_id)
154
155 context.user_data["status_id"] = status["id"]
156 context.user_data["status_content"] = content
157
158 print("status_id", context.user_data["status_id"])
159
160 await query.message.reply_text(
161 text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}",
162 parse_mode=telegram.constants.ParseMode.MARKDOWN,
163 )
164
165 prompt_attach_comment_msg = await query.message.reply_text(PROMPT_ADD_COMMENT, reply_markup=INLINE_SKIP_MENU)
166 context.user_data[PROMPT_ADD_COMMENT] = prompt_attach_comment_msg.message_id
167
168 return ADD_COMMENT
169
170
171async def callback_location_keyword_search(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
172 await context.bot.delete_message(update.effective_chat.id, context.user_data[PROMPT_LOCATION_KEYWORD])
173
174 location_search = update.effective_message.text
175 latitude = context.user_data["latitude"]
176 longitude = context.user_data["longitude"]
177
178 keyboard = []
179 poi_result = query_poi(location_search, latitude, longitude)
180 if len(poi_result) == 0:
181 poi_result = query_poi("", latitude, longitude)
182
183 for poi in poi_result:
184 keyboard.append([
185 InlineKeyboardButton(poi["name"], callback_data=poi["fsq_id"]),
186 ])
187
188 if len(keyboard) == 0:
189 await update.message.reply_text(PROMPT_NO_NEARBY_POI)
190 return LOCATION_CONFIRMATION
191 else:
192 reply_markup = InlineKeyboardMarkup(keyboard)
193 context.user_data["location_search"] = location_search
194 await update.message.reply_text(PROMPT_CHOOSE_POI_FROM_LIST, reply_markup=reply_markup)
195
196 return LOCATION_CONFIRMATION
197
198
199async def callback_skip_location_keyword(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
200 query = update.callback_query
201 await query.answer()
202
203 await query.message.delete()
204 latitude = context.user_data["latitude"]
205 longitude = context.user_data["longitude"]
206
207 keyboard = []
208
209 for poi in query_poi("", latitude, longitude):
210 keyboard.append([
211 InlineKeyboardButton(poi["name"], callback_data=poi["fsq_id"]),
212 ])
213
214 reply_markup = InlineKeyboardMarkup(keyboard)
215 await query.message.reply_text(PROMPT_CHOOSE_POI_FROM_LIST, reply_markup=reply_markup)
216
217 return LOCATION_CONFIRMATION
218
219
220async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
221 await context.bot.delete_message(update.effective_chat.id, context.user_data[PROMPT_ADD_COMMENT])
222 comment = update.effective_message.text
223
224 mastodon_client.status_update(
225 status=f"{comment} " + context.user_data["status_content"],
226 id=context.user_data["status_id"])
227
228 context.user_data["status_content"] = f"{comment} " + context.user_data["status_content"]
229 prompt_attach_photo_msg = await update.message.reply_text(PROMPT_ADD_MEDIA, reply_markup=INLINE_SKIP_MENU)
230 context.user_data[PROMPT_ADD_MEDIA] = prompt_attach_photo_msg.message_id
231
232 return ADD_MEDIA
233
234
235async def callback_skip_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
236 await context.bot.delete_message(update.effective_chat.id, context.user_data[PROMPT_ADD_COMMENT])
237 prompt_attach_photo_msg = await update.message.reply_text(PROMPT_ADD_MEDIA, reply_markup=INLINE_SKIP_MENU)
238 context.user_data[PROMPT_ADD_MEDIA] = prompt_attach_photo_msg.message_id
239 return ADD_MEDIA
240
241
242async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
243 await update.message.reply_text(PROMPT_HELP)
244
245
246async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
247 await update.message.reply_text(text=PROMPT_CANCELED, reply_markup=MAIN_MENU)
248
249 return ConversationHandler.END
250
251
252async def process_media_group(context: CallbackContext):
253 context.job.data = cast(List[MsgDict], context.job.data)
254
255 media_id = []
256 chat_id = context.job.data[0].get("chat_id")
257 for msg_dict in context.job.data:
258 if len(media_id) >= 4:
259 await context.bot.send_message(chat_id=chat_id, text=PROMPT_MAX_PHOTO_REACHED, reply_markup=MAIN_MENU)
260 return
261
262 file = await context.bot.get_file(msg_dict.get("media_id"))
263 img = io.BytesIO()
264 await file.download_to_memory(img)
265
266 img.seek(0)
267
268 media = mastodon_client.media_post(img.read(), mime_type="image/jpeg")
269 media_id.append(media["id"])
270
271 mastodon_client.status_update(
272 status=msg_dict.get("content"),
273 id=msg_dict.get("status_id"),
274 media_ids=media_id)
275
276 await context.bot.send_message(chat_id=chat_id, text=PROMPT_DONE, reply_markup=MAIN_MENU)
277
278
279async def callback_add_media(update: Update, context: CallbackContext):
280 await update.message.reply_chat_action(telegram.constants.ChatAction.TYPING)
281
282 try:
283 await context.bot.delete_message(chat_id=update.message.chat_id,
284 message_id=context.user_data[PROMPT_ADD_MEDIA])
285 except telegram.error.BadRequest as e:
286 if "not found" in str(e.message):
287 pass
288
289 status_id = context.user_data["status_id"]
290 status_content = context.user_data["status_content"]
291
292 message = update.effective_message
293 context.user_data["media"] = []
294 if message.media_group_id:
295 media_id = message.photo[-1].file_id if message.photo else message.effective_attachment.file_id
296 msg_dict = {
297 "media_id": media_id,
298 "caption": message.caption_html,
299 "status_id": status_id,
300 "content": status_content,
301 "chat_id": message.chat_id,
302 }
303 jobs = context.job_queue.get_jobs_by_name(str(message.media_group_id))
304 if jobs:
305 jobs[0].data.append(msg_dict)
306 else:
307 context.job_queue.run_once(callback=process_media_group, when=MEDIA_GROUP_TIMEOUT,
308 data=[msg_dict], name=str(message.media_group_id))
309 else:
310 file = await update.message.effective_attachment[-1].get_file()
311 img = io.BytesIO()
312 await file.download_to_memory(img)
313 img.seek(0)
314
315 media = mastodon_client.media_post(img.read(), mime_type="image/jpeg")
316 mastodon_client.status_update(
317 status=status_content,
318 id=status_id,
319 media_ids=media["id"])
320
321 await update.message.reply_text(text=PROMPT_DONE, reply_markup=MAIN_MENU)
322
323
324async def callback_skip_media(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
325 query = update.callback_query
326 await query.answer()
327
328 await query.delete_message()
329 await query.message.reply_text(text=PROMPT_DONE, reply_markup=MAIN_MENU)
330
331 return ConversationHandler.END
332
333 12
334def main() -> None: 13def main() -> None:
335 application = Application.builder().token(BOT_TOKEN).build() 14 application = Application.builder().token(BOT_TOKEN).build()
336 15
337 checkin_handler = ConversationHandler( 16 checkin_handler = ConversationHandler(
338 entry_points=[ 17 entry_points=[
339 CommandHandler("start", start), 18 CommandHandler("start", start_command),
340 MessageHandler(filters.LOCATION, callback_location_sharing), 19 MessageHandler(filters.LOCATION, callback_location_sharing),
341 ], 20 ],
342 states={ 21 states={
@@ -358,7 +37,7 @@ def main() -> None:
358 ADD_MEDIA: [MessageHandler(filters.PHOTO, callback_add_media), 37 ADD_MEDIA: [MessageHandler(filters.PHOTO, callback_add_media),
359 CallbackQueryHandler(callback_skip_media)], 38 CallbackQueryHandler(callback_skip_media)],
360 }, 39 },
361 fallbacks=[CommandHandler("cancel", cancel)], 40 fallbacks=[CommandHandler("cancel", cancel_command)],
362 per_message=False, 41 per_message=False,
363 allow_reentry=True, 42 allow_reentry=True,
364 ) 43 )
Powered by cgit v1.2.3 (git 2.41.0)