mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 22:45:09 +01:00
36d49ea9cd
* Render-fixes for BP * docs: fix simple typo, submition -> submission (#2260) There is a small typo in tests/test_bot.py. Should read `submission` rather than `submition`. * Type on rawapibot.py docstring * typo * Typo: Filters.document(s) * Typo fix * Doc fix for messageentity (#2311) * Add New Shortcuts to Chat (#2291) * Add shortcuts * Add a note * Add run_async Parameter to ConversationHandler (#2292) * Add run_async parameter * Update docstring * Update test to explicitly specify parameter * Fix test job queue * Add version added tag to docs * Update docstring Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com> * Doc nitpicking Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com> Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de> * Fix rendering in messageentity Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de> Co-authored-by: zeshuaro <joshuaystang@gmail.com> Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com> * fix: type hints for TelegramError changed :class:`telegram.TelegramError` to :class:`telegram.error.TelegramError` * fix: the error can be more then just a Telegram error * Doc fix for inlinekeyboardbutton.py added missing colon which broke rendering * fix: remove context argument and doc remark look at us already being in post 12 * use rtd badge * filters doc fixes * fix some rendering * Doc & Rendering fixes for helpers.py Co-authored-by: Tim Gates <tim.gates@iress.com> Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com> Co-authored-by: zeshuaro <joshuaystang@gmail.com> Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com> Co-authored-by: Harshil <ilovebhagwan@gmail.com>
59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
#!/usr/bin/env python
|
|
# pylint: disable=W0603
|
|
"""Simple Bot to reply to Telegram messages.
|
|
|
|
This is built on the API wrapper, see echobot.py to see the same example built
|
|
on the telegram.ext bot framework.
|
|
This program is dedicated to the public domain under the CC0 license.
|
|
"""
|
|
import logging
|
|
from typing import NoReturn
|
|
from time import sleep
|
|
|
|
import telegram
|
|
from telegram.error import NetworkError, Unauthorized
|
|
|
|
|
|
UPDATE_ID = None
|
|
|
|
|
|
def main() -> NoReturn:
|
|
"""Run the bot."""
|
|
global UPDATE_ID
|
|
# Telegram Bot Authorization Token
|
|
bot = telegram.Bot('TOKEN')
|
|
|
|
# get the first pending update_id, this is so we can skip over it in case
|
|
# we get an "Unauthorized" exception.
|
|
try:
|
|
UPDATE_ID = bot.get_updates()[0].update_id
|
|
except IndexError:
|
|
UPDATE_ID = None
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
while True:
|
|
try:
|
|
echo(bot)
|
|
except NetworkError:
|
|
sleep(1)
|
|
except Unauthorized:
|
|
# The user has removed or blocked the bot.
|
|
UPDATE_ID += 1 # type: ignore[operator]
|
|
|
|
|
|
def echo(bot: telegram.Bot) -> None:
|
|
"""Echo the message the user sent."""
|
|
global UPDATE_ID
|
|
# Request updates after the last update_id
|
|
for update in bot.get_updates(offset=UPDATE_ID, timeout=10):
|
|
UPDATE_ID = update.update_id + 1
|
|
|
|
if update.message: # your bot can receive updates without messages
|
|
if update.message.text: # not all messages contain text
|
|
# Reply to the message
|
|
update.message.reply_text(update.message.text)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|