diff options
author | clarkzjw <[email protected]> | 2023-02-20 13:49:51 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-02-20 13:49:51 -0800 |
commit | f927324709d639adfcd5487c7740e3d60f2246b8 (patch) | |
tree | 125b6ec833af57b2ec1813309506dbe6b791b35f /bot.py | |
parent | a099e6cf153ae7155957bb16bc56233d2420288c (diff) | |
parent | f7f99f41e768e6740adbf2ee708488b29fe6265a (diff) | |
download | swarm2fediverse-f927324709d639adfcd5487c7740e3d60f2246b8.tar.gz |
basic features
Diffstat (limited to 'bot.py')
-rw-r--r-- | bot.py | 49 |
1 files changed, 32 insertions, 17 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: |
@@ -22,7 +23,12 @@ if __version_info__ < (20, 0, 0, "alpha", 1): | |||
22 | f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html" | 23 | f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html" |
23 | ) | 24 | ) |
24 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update | 25 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update |
25 | from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes | 26 | from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters |
27 | |||
28 | from config import BOT_TOKEN | ||
29 | from foursquare.query_poi import query_poi | ||
30 | from dbstore.dbm_store import get_loc | ||
31 | from toot import mastodon_client | ||
26 | 32 | ||
27 | # Enable logging | 33 | # Enable logging |
28 | logging.basicConfig( | 34 | logging.basicConfig( |
@@ -32,29 +38,39 @@ logger = logging.getLogger(__name__) | |||
32 | 38 | ||
33 | 39 | ||
34 | async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | 40 | async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
35 | """Sends a message with three inline buttons attached.""" | 41 | hello = "Hello, this is `checkin.bot`. \n\n" \ |
36 | keyboard = [ | 42 | "This is a Telegram bot with functionality similar to Foursquare Swarm, " \ |
37 | [ | 43 | "but check in and post your location to the Fediverse (Mastodon/Pleroma) instead of Twitter.\n\n" \ |
38 | InlineKeyboardButton("Option 1", callback_data="1"), | 44 | "Aware of privacy concerns, this bot will not store your location data."\ |
39 | InlineKeyboardButton("Option 2", callback_data="2"), | 45 | "*Be safe and cautious when sharing your real time location on the web.* \n\n"\ |
40 | ], | 46 | "Start using this bot by sharing your location using Telegram context menu to it." |
41 | [InlineKeyboardButton("Option 3", callback_data="3")], | ||
42 | ] | ||
43 | 47 | ||
44 | reply_markup = InlineKeyboardMarkup(keyboard) | 48 | await update.message.reply_text(hello, parse_mode=telegram.constants.ParseMode.MARKDOWN) |
45 | 49 | ||
46 | await update.message.reply_text("Please choose:", reply_markup=reply_markup) | 50 | |
51 | async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | ||
52 | keyboard = [] | ||
53 | |||
54 | for poi in query_poi(update.message.location.latitude, update.message.location.longitude): | ||
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) | ||
47 | 61 | ||
48 | 62 | ||
49 | async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | 63 | async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
50 | """Parses the CallbackQuery and updates the message text.""" | 64 | """Parses the CallbackQuery and updates the message text.""" |
51 | query = update.callback_query | 65 | query = update.callback_query |
52 | 66 | ||
53 | # CallbackQueries need to be answered, even if no notification to the user is needed | ||
54 | # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery | ||
55 | 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") | ||
56 | 72 | ||
57 | await query.edit_message_text(text=f"Selected option: {query.data}") | 73 | await query.edit_message_text(text=f"Selected option: {poi}") |
58 | 74 | ||
59 | 75 | ||
60 | async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | 76 | async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
@@ -63,12 +79,11 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No | |||
63 | 79 | ||
64 | 80 | ||
65 | def main() -> None: | 81 | def main() -> None: |
66 | """Run the bot.""" | 82 | application = Application.builder().token(BOT_TOKEN).build() |
67 | # Create the Application and pass it your bot's token. | ||
68 | application = Application.builder().token("TOKEN").build() | ||
69 | 83 | ||
70 | application.add_handler(CommandHandler("start", start)) | 84 | application.add_handler(CommandHandler("start", start)) |
71 | application.add_handler(CallbackQueryHandler(button)) | 85 | application.add_handler(CallbackQueryHandler(button)) |
86 | application.add_handler(MessageHandler(filters.LOCATION & ~filters.COMMAND, checkin)) | ||
72 | application.add_handler(CommandHandler("help", help_command)) | 87 | application.add_handler(CommandHandler("help", help_command)) |
73 | 88 | ||
74 | # Run the bot until the user presses Ctrl-C | 89 | # Run the bot until the user presses Ctrl-C |