test.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python3
  2. import argparse
  3. import io
  4. import logging
  5. import pathlib
  6. import requests
  7. import telegram.ext
  8. import telegram.update
  9. _LOGGER = logging.getLogger(__name__)
  10. def _start_command(
  11. update: telegram.update.Update,
  12. context: telegram.ext.callbackcontext.CallbackContext,
  13. ):
  14. if "last_photo_message_id" in context.chat_data:
  15. update.effective_chat.send_message(text="Lösung:", disable_notification=True)
  16. # https://github.com/python-telegram-bot/python-telegram-bot/pull/2043
  17. context.bot.send_location(
  18. chat_id=update.effective_chat.id,
  19. latitude=48,
  20. longitude=16,
  21. reply_to_message_id=context.chat_data["last_photo_message_id"],
  22. )
  23. update.effective_chat.send_message(
  24. text="Neues Photo wird ausgewählt und gesendet.", disable_notification=True
  25. )
  26. photo_request = requests.get(
  27. "https://upload.wikimedia.org/wikipedia/commons/c/cf/Clematis_alpina_02.jpg"
  28. )
  29. photo_message = update.effective_chat.send_photo(
  30. photo=io.BytesIO(photo_request.content),
  31. caption="Wo wurde dieses Photo aufgenommen?",
  32. )
  33. context.chat_data["last_photo_message_id"] = photo_message.message_id
  34. def _main():
  35. logging.basicConfig(
  36. level=logging.DEBUG,
  37. format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s",
  38. datefmt="%Y-%m-%dT%H:%M:%S%z",
  39. )
  40. argparser = argparse.ArgumentParser()
  41. argparser.add_argument("--token-path", type=pathlib.Path, required=True)
  42. args = argparser.parse_args()
  43. _LOGGER.debug("args=%r", args)
  44. updater = telegram.ext.Updater(
  45. token=args.token_path.read_text().rstrip(), use_context=True
  46. )
  47. updater.dispatcher.add_handler(telegram.ext.CommandHandler("start", _start_command))
  48. updater.start_polling()
  49. if __name__ == "__main__":
  50. _main()