mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-21 22:56:38 +01:00
cleaned up code
This commit is contained in:
parent
0a27e3927b
commit
3162bc60e9
1 changed files with 22 additions and 11 deletions
|
@ -12,28 +12,32 @@ inserted into the update queue for the bot to handle.
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from telegram import BotEventHandler
|
from telegram import BotEventHandler
|
||||||
from telegram.boteventhandler import run_async
|
from telegram.broadcaster import run_async
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import re
|
import re
|
||||||
|
|
||||||
global last_chat_id
|
|
||||||
last_chat_id = 0
|
last_chat_id = 0
|
||||||
|
|
||||||
|
|
||||||
def removeCommand(text):
|
def removeCommand(text):
|
||||||
return ' '.join(text.split(' ')[1:])
|
return ' '.join(text.split(' ')[1:])
|
||||||
|
|
||||||
""" Command Handlers """
|
|
||||||
|
# Command Handlers
|
||||||
def startCommandHandler(bot, update):
|
def startCommandHandler(bot, update):
|
||||||
bot.sendMessage(update.message.chat_id, text='Hi!')
|
bot.sendMessage(update.message.chat_id, text='Hi!')
|
||||||
|
|
||||||
|
|
||||||
def helpCommandHandler(bot, update):
|
def helpCommandHandler(bot, update):
|
||||||
bot.sendMessage(update.message.chat_id, text='Help!')
|
bot.sendMessage(update.message.chat_id, text='Help!')
|
||||||
|
|
||||||
|
|
||||||
def anyMessageHandler(bot, update):
|
def anyMessageHandler(bot, update):
|
||||||
print("chat_id: %d\nFrom: %s\nText: %s" %
|
print("chat_id: %d\nFrom: %s\nText: %s" %
|
||||||
(update.message.chat_id, str(update.message.from_user),
|
(update.message.chat_id, str(update.message.from_user),
|
||||||
update.message.text))
|
update.message.text))
|
||||||
|
|
||||||
|
|
||||||
def unknownCommandHandler(bot, update):
|
def unknownCommandHandler(bot, update):
|
||||||
bot.sendMessage(update.message.chat_id, text='Command not recognized!')
|
bot.sendMessage(update.message.chat_id, text='Command not recognized!')
|
||||||
|
|
||||||
|
@ -52,42 +56,49 @@ def messageHandler(bot, update):
|
||||||
sleep(2) # IO-heavy operation here
|
sleep(2) # IO-heavy operation here
|
||||||
bot.sendMessage(update.message.chat_id, text=update.message.text)
|
bot.sendMessage(update.message.chat_id, text=update.message.text)
|
||||||
|
|
||||||
|
|
||||||
def errorHandler(bot, error):
|
def errorHandler(bot, error):
|
||||||
print(str(error))
|
print(str(error))
|
||||||
|
|
||||||
|
|
||||||
def CLIReplyCommandHandler(bot, update):
|
def CLIReplyCommandHandler(bot, update):
|
||||||
if last_chat_id is not 0:
|
if last_chat_id is not 0:
|
||||||
bot.sendMessage(chat_id=last_chat_id, text=removeCommand(update))
|
bot.sendMessage(chat_id=last_chat_id, text=removeCommand(update))
|
||||||
|
|
||||||
|
|
||||||
def unknownCLICommandHandler(bot, update):
|
def unknownCLICommandHandler(bot, update):
|
||||||
print("Command not found: %s" % update)
|
print("Command not found: %s" % update)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Create the EventHandler and pass it your bot's token
|
# Create the EventHandler and pass it your bot's token
|
||||||
eh = BotEventHandler("TOKEN")
|
eh = BotEventHandler("TOKEN")
|
||||||
|
|
||||||
|
# Get the broadcaster to register handlers
|
||||||
|
bc = eh.broadcaster
|
||||||
|
|
||||||
# on different commands - answer in Telegram
|
# on different commands - answer in Telegram
|
||||||
eh.broadcaster.addTelegramCommandHandler("start", startCommandHandler)
|
bc.addTelegramCommandHandler("start", startCommandHandler)
|
||||||
eh.broadcaster.addTelegramCommandHandler("help", helpCommandHandler)
|
bc.addTelegramCommandHandler("help", helpCommandHandler)
|
||||||
|
|
||||||
# on regex match - print all messages to stdout
|
# on regex match - print all messages to stdout
|
||||||
eh.broadcaster.addTelegramRegexHandler(re.compile('.*'), anyMessageHandler)
|
bc.addTelegramRegexHandler(re.compile('.*'), anyMessageHandler)
|
||||||
|
|
||||||
# on CLI commands - type "/reply text" in terminal to reply to the last
|
# on CLI commands - type "/reply text" in terminal to reply to the last
|
||||||
# active chat
|
# active chat
|
||||||
eh.broadcaster.addStringCommandHandler('reply', CLIReplyCommandHandler)
|
bc.addStringCommandHandler('reply', CLIReplyCommandHandler)
|
||||||
|
|
||||||
# on unknown commands - answer on Telegram
|
# on unknown commands - answer on Telegram
|
||||||
eh.broadcaster.addUnknownTelegramCommandHandler(unknownCommandHandler)
|
bc.addUnknownTelegramCommandHandler(unknownCommandHandler)
|
||||||
|
|
||||||
# on unknown CLI commands - notify the user
|
# on unknown CLI commands - notify the user
|
||||||
eh.broadcaster.addUnknownStringCommandHandler(unknownCLICommandHandler)
|
bc.addUnknownStringCommandHandler(unknownCLICommandHandler)
|
||||||
|
|
||||||
# on noncommand i.e message - echo the message on Telegram
|
# on noncommand i.e message - echo the message on Telegram
|
||||||
eh.broadcaster.addTelegramMessageHandler(messageHandler)
|
bc.addTelegramMessageHandler(messageHandler)
|
||||||
|
|
||||||
# on error - print error to stdout
|
# on error - print error to stdout
|
||||||
eh.broadcaster.addErrorHandler(errorHandler)
|
bc.addErrorHandler(errorHandler)
|
||||||
|
|
||||||
# Start the Bot and store the update Queue,
|
# Start the Bot and store the update Queue,
|
||||||
# so we can insert updates ourselves
|
# so we can insert updates ourselves
|
||||||
|
|
Loading…
Reference in a new issue