#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
Basic example for a bot that uses inline keyboards. For an in-depth explanation, check out
https://github.com/python-telegram-bot/python-telegram-bot/wiki/InlineKeyboard-Example.
"""
import logging
import telegram.constants
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters
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(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
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(hello, parse_mode=telegram.constants.ParseMode.MARKDOWN)
async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
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
await query.answer()
poi = get_loc(query.data)
status = mastodon_client.status_post(
f"I'm at {poi['name']} in {poi['locality']}, {poi['region']}, {poi['osm_url']}",
visibility="private")
await query.edit_message_text(text=f"Selected place: {poi['name']}\nPosted to Mastodon: {status['url']}",
parse_mode=telegram.constants.ParseMode.MARKDOWN)
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Displays info on how to use the bot."""
await update.message.reply_text("Use /start to test this bot.")
def main() -> None:
application = Application.builder().token(BOT_TOKEN).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(CallbackQueryHandler(button))
application.add_handler(MessageHandler(filters.LOCATION & ~filters.COMMAND, checkin))
application.add_handler(CommandHandler("help", help_command))
# Run the bot until the user presses Ctrl-C
application.run_polling()
if __name__ == "__main__":
main()