mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-25 00:27:46 +01:00
update example
This commit is contained in:
parent
12201f392d
commit
ae59a18e2d
1 changed files with 24 additions and 7 deletions
|
@ -46,7 +46,7 @@ def unknownCommandHandler(bot, update):
|
||||||
def messageHandler(bot, update):
|
def messageHandler(bot, update):
|
||||||
"""
|
"""
|
||||||
Example for an asynchronous handler. It's not guaranteed that replies will
|
Example for an asynchronous handler. It's not guaranteed that replies will
|
||||||
be in order when using @run_async
|
be in order when using @run_async.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Save last chat_id to use in reply handler
|
# Save last chat_id to use in reply handler
|
||||||
|
@ -66,13 +66,21 @@ def CLIReplyCommandHandler(bot, update):
|
||||||
bot.sendMessage(chat_id=last_chat_id, text=removeCommand(update))
|
bot.sendMessage(chat_id=last_chat_id, text=removeCommand(update))
|
||||||
|
|
||||||
|
|
||||||
|
def anyCLIHandler(bot, update, update_queue):
|
||||||
|
"""
|
||||||
|
You can get the update queue as an argument in any handler just by adding
|
||||||
|
it to the argument list. Be careful with this though.
|
||||||
|
"""
|
||||||
|
update_queue.put('/%s' % 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", workers=2)
|
||||||
|
|
||||||
# Get the broadcaster to register handlers
|
# Get the broadcaster to register handlers
|
||||||
bc = eh.broadcaster
|
bc = eh.broadcaster
|
||||||
|
@ -82,7 +90,7 @@ def main():
|
||||||
bc.addTelegramCommandHandler("help", helpCommandHandler)
|
bc.addTelegramCommandHandler("help", helpCommandHandler)
|
||||||
|
|
||||||
# on regex match - print all messages to stdout
|
# on regex match - print all messages to stdout
|
||||||
bc.addTelegramRegexHandler(re.compile('.*'), anyMessageHandler)
|
bc.addTelegramRegexHandler('.*', 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
|
||||||
|
@ -94,6 +102,9 @@ def main():
|
||||||
# on unknown CLI commands - notify the user
|
# on unknown CLI commands - notify the user
|
||||||
bc.addUnknownStringCommandHandler(unknownCLICommandHandler)
|
bc.addUnknownStringCommandHandler(unknownCLICommandHandler)
|
||||||
|
|
||||||
|
# on any CLI message that is not a command - resend it as a command
|
||||||
|
bc.addStringRegexHandler('[^/].*', anyCLIHandler)
|
||||||
|
|
||||||
# on noncommand i.e message - echo the message on Telegram
|
# on noncommand i.e message - echo the message on Telegram
|
||||||
bc.addTelegramMessageHandler(messageHandler)
|
bc.addTelegramMessageHandler(messageHandler)
|
||||||
|
|
||||||
|
@ -102,7 +113,7 @@ def main():
|
||||||
|
|
||||||
# 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
|
||||||
update_queue = eh.start()
|
update_queue = eh.start(poll_interval=0.1, timeout=20)
|
||||||
|
|
||||||
# Start CLI-Loop
|
# Start CLI-Loop
|
||||||
while True:
|
while True:
|
||||||
|
@ -110,8 +121,14 @@ def main():
|
||||||
text = raw_input()
|
text = raw_input()
|
||||||
elif sys.version_info.major is 3:
|
elif sys.version_info.major is 3:
|
||||||
text = input()
|
text = input()
|
||||||
|
|
||||||
if len(text) > 0:
|
# Gracefully stop the event handler
|
||||||
|
if text == 'stop':
|
||||||
|
eh.stop()
|
||||||
|
break
|
||||||
|
|
||||||
|
# else, put the text into the update queue
|
||||||
|
elif len(text) > 0:
|
||||||
update_queue.put(text) # Put command into queue
|
update_queue.put(text) # Put command into queue
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue