Advertisement
Guest User

Webhook Telebot Flask

a guest
Aug 6th, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. from flask import Flask
  4. import telebot
  5.  
  6. app = Flask(__name__)
  7.  
  8. # settings for webhook
  9. API_TOKEN = '<здесь мой токен от бота>'
  10.  
  11. WEBHOOK_HOST = 'blablabla.ru'  # здесь мой сайт с SSL сертификатом и оценкой А от ssllabs.com/ssltest/
  12. WEBHOOK_PORT = 8443  # 443, 80, 88 or 8443 (port need to be 'open')
  13. WEBHOOK_LISTEN = '0.0.0.0'  # In some VPS you may need to put here the IP addr
  14.  
  15. # When asked for "Common Name (e.g. server FQDN or YOUR name)" you should reply
  16. # with the same value in you put in WEBHOOK_HOST
  17.  
  18. WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
  19. WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
  20.  
  21.  
  22. bot = telebot.TeleBot(API_TOKEN)
  23.  
  24.  
  25. @bot.message_handler(func=lambda message: True, content_types=['text'])
  26. def echo_message(message):
  27.     bot.reply_to(message, message.text)
  28.  
  29.  
  30. # Empty webserver index, return nothing, just http 200
  31. @app.route('/', methods=['GET', 'HEAD'])
  32. def index():
  33.     return 'Hello, it is Flask'
  34.  
  35.  
  36. # Process webhook calls
  37. @app.route(WEBHOOK_URL_PATH, methods=['POST'])
  38. def webhook():
  39.     if flask.request.headers.get('content-type') == 'application/json':
  40.         json_string = flask.request.get_data().decode('utf-8')
  41.         update = telebot.types.Update.de_json(json_string)
  42.         bot.process_new_updates([update])
  43.         return ''
  44.     else:
  45.         flask.abort(403)
  46.        
  47.  
  48. # Handle '/start' and '/help'
  49. @bot.message_handler(commands=['help', 'start'])
  50. def send_welcome(message):
  51.     bot.reply_to(message,
  52.                  ("Hi there, I am EchoBot.\n"
  53.                   "I am here to echo your kind words back to you."))
  54.  
  55.                  
  56. # Handle all other messages
  57. @bot.message_handler(func=lambda message: True, content_types=['text'])
  58. def echo_message(message):
  59.     bot.reply_to(message, message.text)
  60.  
  61.  
  62. # Remove webhook, it fails sometimes the set if there is a previous webhook
  63. bot.remove_webhook()
  64.  
  65. @app.route('/set_webhook', methods=['GET', 'POST'])
  66. def set_webhook():
  67.     s = bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH)
  68.     if s:
  69.         print(s)
  70.         return "webhook setup ok"
  71.     else:
  72.         return "webhook setup failed"
  73.        
  74.        
  75. # Set webhook
  76. bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH)
  77.                 # certificate=open(WEBHOOK_SSL_CERT, 'r'))
  78.  
  79.  
  80. if __name__ == '__main__':
  81.     # Start flask server
  82.     app.run(port=WEBHOOK_PORT,
  83.             host=WEBHOOK_LISTEN,
  84.             debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement