From f7f99f41e768e6740adbf2ee708488b29fe6265a Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Mon, 20 Feb 2023 13:47:26 -0800 Subject: 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 --- bot.py | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'bot.py') diff --git a/bot.py b/bot.py index e010516..398e433 100644 --- a/bot.py +++ b/bot.py @@ -8,6 +8,7 @@ Basic example for a bot that uses inline keyboards. For an in-depth explanation, """ import logging +import telegram.constants from telegram import __version__ as TG_VER try: @@ -26,7 +27,8 @@ from telegram.ext import Application, CallbackQueryHandler, CommandHandler, Cont from config import BOT_TOKEN from foursquare.query_poi import query_poi - +from dbstore.dbm_store import get_loc +from toot import mastodon_client # Enable logging logging.basicConfig( @@ -36,36 +38,39 @@ logger = logging.getLogger(__name__) async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Sends a message with three inline buttons attached.""" - keyboard = [ - [ - InlineKeyboardButton("Option 1", callback_data="1"), - InlineKeyboardButton("Option 2", callback_data="2"), - ], - [InlineKeyboardButton("Option 3", callback_data="3")], - ] - - reply_markup = InlineKeyboardMarkup(keyboard) + hello = "Hello, this is `checkin.bot`. \n\n" \ + "This is a Telegram bot with functionality similar to Foursquare Swarm, " \ + "but check in and post your location to the Fediverse (Mastodon/Pleroma) instead of Twitter.\n\n" \ + "Aware of privacy concerns, this bot will not store your location data."\ + "*Be safe and cautious when sharing your real time location on the web.* \n\n"\ + "Start using this bot by sharing your location using Telegram context menu to it." - await update.message.reply_text("Please choose:", reply_markup=reply_markup) + await update.message.reply_text(hello, parse_mode=telegram.constants.ParseMode.MARKDOWN) async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - print(update.message.location.latitude) - print(update.message.location.longitude) - poi = query_poi(update.message.location.latitude, update.message.location.longitude) - await update.message.reply_text("Your location received: {}".format(poi)) + keyboard = [] + + for poi in query_poi(update.message.location.latitude, update.message.location.longitude): + keyboard.append([ + InlineKeyboardButton(poi["name"], callback_data=poi["fsq_id"]), + ]) + + reply_markup = InlineKeyboardMarkup(keyboard) + await update.message.reply_text("Select a place", reply_markup=reply_markup) async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Parses the CallbackQuery and updates the message text.""" query = update.callback_query - # CallbackQueries need to be answered, even if no notification to the user is needed - # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery await query.answer() + poi = get_loc(query.data) + + mastodon_client.status_post(f"I'm at {poi['name']} in {poi['locality']}, {poi['region']}, \n[OSM]({poi['osm_url']})", + visibility="private") - await query.edit_message_text(text=f"Selected option: {query.data}") + await query.edit_message_text(text=f"Selected option: {poi}") 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 def main() -> None: - """Run the bot.""" - # Create the Application and pass it your bot's token. application = Application.builder().token(BOT_TOKEN).build() application.add_handler(CommandHandler("start", start)) - application.add_handler(CommandHandler("checkin", checkin)) application.add_handler(CallbackQueryHandler(button)) - # on non command i.e message - echo the message on Telegram application.add_handler(MessageHandler(filters.LOCATION & ~filters.COMMAND, checkin)) application.add_handler(CommandHandler("help", help_command)) -- cgit v1.2.3