1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
pyTelegramBotAPI/examples/webhook_examples/webhook_flask_heroku_echo.py

36 lines
876 B
Python
Raw Normal View History

2016-05-24 22:42:25 +03:00
import os
2018-02-16 18:52:37 +03:00
import telebot
from flask import Flask, request
2016-05-24 22:42:25 +03:00
2018-02-16 18:52:37 +03:00
TOKEN = '<api_token>'
bot = telebot.TeleBot(TOKEN)
2016-05-24 22:42:25 +03:00
server = Flask(__name__)
2018-02-16 18:52:37 +03:00
2016-05-24 22:42:25 +03:00
@bot.message_handler(commands=['start'])
def start(message):
bot.reply_to(message, 'Hello, ' + message.from_user.first_name)
2018-02-16 18:52:37 +03:00
2016-05-24 22:42:25 +03:00
@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
bot.reply_to(message, message.text)
2018-02-16 18:52:37 +03:00
@server.route('/' + TOKEN, methods=['POST'])
2016-05-24 22:42:25 +03:00
def getMessage():
2016-07-11 16:29:25 +03:00
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
2016-05-24 22:42:25 +03:00
return "!", 200
2018-02-16 18:52:37 +03:00
2016-05-24 22:42:25 +03:00
@server.route("/")
def webhook():
bot.remove_webhook()
2018-02-16 18:52:37 +03:00
bot.set_webhook(url='https://your_heroku_project.com/' + TOKEN)
2016-05-24 22:42:25 +03:00
return "!", 200
2018-02-16 18:52:37 +03:00
if __name__ == "__main__":
server.run(host="0.0.0.0", port=int(os.environ.get('PORT', 5000)))