mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-21 22:56:38 +01:00
76 lines
1.8 KiB
Python
76 lines
1.8 KiB
Python
#!/usr/bin/env python
|
|
|
|
"""
|
|
This Bot uses the BotEventHandler class to handle the bot.
|
|
|
|
First, a few handler functions are defined. Then, those functions are passed to
|
|
the Dispatcher and registered at their respective places.
|
|
Then, the bot is started and the CLI-Loop is entered.
|
|
|
|
Usage:
|
|
Basic Echobot example, repeats messages.
|
|
Type 'stop' on the command line to stop the bot.
|
|
"""
|
|
|
|
from telegram import Updater
|
|
import logging
|
|
import sys
|
|
from time import sleep
|
|
|
|
# Enable logging
|
|
root = logging.getLogger()
|
|
root.setLevel(logging.INFO)
|
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
|
ch.setLevel(logging.DEBUG)
|
|
formatter = \
|
|
logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
ch.setFormatter(formatter)
|
|
root.addHandler(ch)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Command Handlers
|
|
def start(bot, update):
|
|
bot.sendMessage(update.message.chat_id, text='Hi!')
|
|
|
|
|
|
def help(bot, update):
|
|
bot.sendMessage(update.message.chat_id, text='Help!')
|
|
|
|
|
|
def echo(bot, update):
|
|
bot.sendMessage(update.message.chat_id, text=update.message.text)
|
|
|
|
|
|
def error(bot, update, error):
|
|
logger.warn('Update "%s" caused error "%s"' % (update, error))
|
|
|
|
|
|
def main():
|
|
# Create the EventHandler and pass it your bot's token.
|
|
updater = Updater("TOKEN")
|
|
|
|
# Get the dispatcher to register handlers
|
|
dp = updater.dispatcher
|
|
|
|
# on different commands - answer in Telegram
|
|
dp.addTelegramCommandHandler("start", start)
|
|
dp.addTelegramCommandHandler("help", help)
|
|
|
|
# on noncommand i.e message - echo the message on Telegram
|
|
dp.addTelegramMessageHandler(echo)
|
|
|
|
# on error - print error to stdout
|
|
dp.addErrorHandler(error)
|
|
|
|
# Start the Bot
|
|
updater.start_polling(timeout=5)
|
|
|
|
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
|
|
# SIGTERM or SIGABRT
|
|
updater.idle()
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|