test.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. # https://github.com/python-telegram-bot/python-telegram-bot/pull/2043
  16. context.bot.send_location(
  17. chat_id=update.effective_chat.id,
  18. latitude=48,
  19. longitude=16,
  20. reply_to_message_id=context.chat_data["last_photo_message_id"],
  21. )
  22. update.effective_chat.send_message(text=f"chat_data={context.chat_data}")
  23. photo_request = requests.get(
  24. "https://upload.wikimedia.org/wikipedia/commons/c/cf/Clematis_alpina_02.jpg"
  25. )
  26. photo_message = update.effective_chat.send_photo(
  27. photo=io.BytesIO(photo_request.content)
  28. )
  29. context.chat_data["last_photo_message_id"] = photo_message.message_id
  30. def _main():
  31. logging.basicConfig(
  32. level=logging.DEBUG,
  33. format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s",
  34. datefmt="%Y-%m-%dT%H:%M:%S%z",
  35. )
  36. argparser = argparse.ArgumentParser()
  37. argparser.add_argument("--token-path", type=pathlib.Path, required=True)
  38. args = argparser.parse_args()
  39. _LOGGER.debug("args=%r", args)
  40. updater = telegram.ext.Updater(
  41. token=args.token_path.read_text().rstrip(), use_context=True
  42. )
  43. updater.dispatcher.add_handler(telegram.ext.CommandHandler("start", _start_command))
  44. updater.start_polling()
  45. if __name__ == "__main__":
  46. _main()