-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0189442
commit d6f8077
Showing
5 changed files
with
119 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
"""Simple Bot to reply to Telegram messages. | ||
This is built on the API wrapper, see rawapibot.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 | ||
import telegram | ||
from telegram.error import NetworkError, Unauthorized | ||
from time import sleep | ||
|
||
|
||
update_id = None | ||
|
||
|
||
def main(): | ||
"""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 | ||
|
||
|
||
def echo(bot): | ||
"""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 | ||
# Reply to the message | ||
update.message.reply_text(update.message.text) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |