Skip to content

Instantly share code, notes, and snippets.

@zaoldyeck
Last active June 21, 2018 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaoldyeck/1b596f6a1d8ac56eaf1db5953dd0531f to your computer and use it in GitHub Desktop.
Save zaoldyeck/1b596f6a1d8ac56eaf1db5953dd0531f to your computer and use it in GitHub Desktop.
import configparser
import logging
import telegram
from flask import Flask, request
from telegram.ext import Dispatcher, MessageHandler, Filters
# Load data from config.ini file
config = configparser.ConfigParser()
config.read('config.ini')
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Initial Flask app
app = Flask(__name__)
# Initial bot by Telegram access token
bot = telegram.Bot(token=(config['TELEGRAM']['ACCESS_TOKEN']))
@app.route('/hook', methods=['POST'])
def webhook_handler():
"""Set route /hook with POST method will trigger this method."""
if request.method == "POST":
update = telegram.Update.de_json(request.get_json(force=True), bot)
# Update dispatcher process that handler to process this message
dispatcher.process_update(update)
return 'ok'
def reply_handler(bot, update):
"""Reply message."""
text = update.message.text
update.message.reply_text(text)
# New a dispatcher for bot
dispatcher = Dispatcher(bot, None)
# Add handler for handling message, there are many kinds of message. For this handler, it particular handle text
# message.
dispatcher.add_handler(MessageHandler(Filters.text, reply_handler))
if __name__ == "__main__":
# Running server
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment