diff options
author | clarkzjw <[email protected]> | 2023-02-20 13:47:26 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-02-20 13:47:26 -0800 |
commit | f7f99f41e768e6740adbf2ee708488b29fe6265a (patch) | |
tree | 125b6ec833af57b2ec1813309506dbe6b791b35f /bot.py | |
parent | 3a230798be1bc63b363cf75b8b1cae3a508cca84 (diff) | |
download | swarm2fediverse-f7f99f41e768e6740adbf2ee708488b29fe6265a.tar.gz |
implemented basic functions:
- send a location from Telegram to bot
- query a list (7) of POIs from Foursquare
- send user inline keyboard button to choose a location
- post toot status update to Mastodon with a link to OSM
- store previously seen locations in local db
Diffstat (limited to 'bot.py')
-rw-r--r-- | bot.py | 47 |
1 files changed, 24 insertions, 23 deletions
@@ -8,6 +8,7 @@ Basic example for a bot that uses inline keyboards. For an in-depth explanation, | |||
8 | """ | 8 | """ |
9 | import logging | 9 | import logging |
10 | 10 | ||
11 | import telegram.constants | ||
11 | from telegram import __version__ as TG_VER | 12 | from telegram import __version__ as TG_VER |
12 | 13 | ||
13 | try: | 14 | try: |
@@ -26,7 +27,8 @@ from telegram.ext import Application, CallbackQueryHandler, CommandHandler, Cont | |||
26 | 27 | ||
27 | from config import BOT_TOKEN | 28 | from config import BOT_TOKEN |
28 | from foursquare.query_poi import query_poi | 29 | from foursquare.query_poi import query_poi |
29 | 30 | from dbstore.dbm_store import get_loc | |
31 | from toot import mastodon_client | ||
30 | 32 | ||
31 | # Enable logging | 33 | # Enable logging |
32 | logging.basicConfig( | 34 | logging.basicConfig( |
@@ -36,36 +38,39 @@ logger = logging.getLogger(__name__) | |||
36 | 38 | ||
37 | 39 | ||
38 | async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | 40 | async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
39 | """Sends a message with three inline buttons attached.""" | 41 | hello = "Hello, this is `checkin.bot`. \n\n" \ |
40 | keyboard = [ | 42 | "This is a Telegram bot with functionality similar to Foursquare Swarm, " \ |
41 | [ | 43 | "but check in and post your location to the Fediverse (Mastodon/Pleroma) instead of Twitter.\n\n" \ |
42 | InlineKeyboardButton("Option 1", callback_data="1"), | 44 | "Aware of privacy concerns, this bot will not store your location data."\ |
43 | InlineKeyboardButton("Option 2", callback_data="2"), | 45 | "*Be safe and cautious when sharing your real time location on the web.* \n\n"\ |
44 | ], | 46 | "Start using this bot by sharing your location using Telegram context menu to it." |
45 | [InlineKeyboardButton("Option 3", callback_data="3")], | ||
46 | ] | ||
47 | |||
48 | reply_markup = InlineKeyboardMarkup(keyboard) | ||
49 | 47 | ||
50 | await update.message.reply_text("Please choose:", reply_markup=reply_markup) | 48 | await update.message.reply_text(hello, parse_mode=telegram.constants.ParseMode.MARKDOWN) |
51 | 49 | ||
52 | 50 | ||
53 | async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | 51 | async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
54 | print(update.message.location.latitude) | 52 | keyboard = [] |
55 | print(update.message.location.longitude) | 53 | |
56 | poi = query_poi(update.message.location.latitude, update.message.location.longitude) | 54 | for poi in query_poi(update.message.location.latitude, update.message.location.longitude): |
57 | await update.message.reply_text("Your location received: {}".format(poi)) | 55 | keyboard.append([ |
56 | InlineKeyboardButton(poi["name"], callback_data=poi["fsq_id"]), | ||
57 | ]) | ||
58 | |||
59 | reply_markup = InlineKeyboardMarkup(keyboard) | ||
60 | await update.message.reply_text("Select a place", reply_markup=reply_markup) | ||
58 | 61 | ||
59 | 62 | ||
60 | async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | 63 | async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
61 | """Parses the CallbackQuery and updates the message text.""" | 64 | """Parses the CallbackQuery and updates the message text.""" |
62 | query = update.callback_query | 65 | query = update.callback_query |
63 | 66 | ||
64 | # CallbackQueries need to be answered, even if no notification to the user is needed | ||
65 | # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery | ||
66 | await query.answer() | 67 | await query.answer() |
68 | poi = get_loc(query.data) | ||
69 | |||
70 | mastodon_client.status_post(f"I'm at {poi['name']} in {poi['locality']}, {poi['region']}, \n[OSM]({poi['osm_url']})", | ||
71 | visibility="private") | ||
67 | 72 | ||
68 | await query.edit_message_text(text=f"Selected option: {query.data}") | 73 | await query.edit_message_text(text=f"Selected option: {poi}") |
69 | 74 | ||
70 | 75 | ||
71 | async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | 76 | async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
@@ -74,14 +79,10 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No | |||
74 | 79 | ||
75 | 80 | ||
76 | def main() -> None: | 81 | def main() -> None: |
77 | """Run the bot.""" | ||
78 | # Create the Application and pass it your bot's token. | ||
79 | application = Application.builder().token(BOT_TOKEN).build() | 82 | application = Application.builder().token(BOT_TOKEN).build() |
80 | 83 | ||
81 | application.add_handler(CommandHandler("start", start)) | 84 | application.add_handler(CommandHandler("start", start)) |
82 | application.add_handler(CommandHandler("checkin", checkin)) | ||
83 | application.add_handler(CallbackQueryHandler(button)) | 85 | application.add_handler(CallbackQueryHandler(button)) |
84 | # on non command i.e message - echo the message on Telegram | ||
85 | application.add_handler(MessageHandler(filters.LOCATION & ~filters.COMMAND, checkin)) | 86 | application.add_handler(MessageHandler(filters.LOCATION & ~filters.COMMAND, checkin)) |
86 | application.add_handler(CommandHandler("help", help_command)) | 87 | application.add_handler(CommandHandler("help", help_command)) |
87 | 88 | ||