Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from flask import Flask
- import telebot
- app = Flask(__name__)
- # settings for webhook
- API_TOKEN = '<здесь мой токен от бота>'
- WEBHOOK_HOST = 'blablabla.ru' # здесь мой сайт с SSL сертификатом и оценкой А от ssllabs.com/ssltest/
- WEBHOOK_PORT = 8443 # 443, 80, 88 or 8443 (port need to be 'open')
- WEBHOOK_LISTEN = '0.0.0.0' # In some VPS you may need to put here the IP addr
- # When asked for "Common Name (e.g. server FQDN or YOUR name)" you should reply
- # with the same value in you put in WEBHOOK_HOST
- WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
- WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
- bot = telebot.TeleBot(API_TOKEN)
- @bot.message_handler(func=lambda message: True, content_types=['text'])
- def echo_message(message):
- bot.reply_to(message, message.text)
- # Empty webserver index, return nothing, just http 200
- @app.route('/', methods=['GET', 'HEAD'])
- def index():
- return 'Hello, it is Flask'
- # Process webhook calls
- @app.route(WEBHOOK_URL_PATH, methods=['POST'])
- def webhook():
- if flask.request.headers.get('content-type') == 'application/json':
- json_string = flask.request.get_data().decode('utf-8')
- update = telebot.types.Update.de_json(json_string)
- bot.process_new_updates([update])
- return ''
- else:
- flask.abort(403)
- # Handle '/start' and '/help'
- @bot.message_handler(commands=['help', 'start'])
- def send_welcome(message):
- bot.reply_to(message,
- ("Hi there, I am EchoBot.\n"
- "I am here to echo your kind words back to you."))
- # Handle all other messages
- @bot.message_handler(func=lambda message: True, content_types=['text'])
- def echo_message(message):
- bot.reply_to(message, message.text)
- # Remove webhook, it fails sometimes the set if there is a previous webhook
- bot.remove_webhook()
- @app.route('/set_webhook', methods=['GET', 'POST'])
- def set_webhook():
- s = bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH)
- if s:
- print(s)
- return "webhook setup ok"
- else:
- return "webhook setup failed"
- # Set webhook
- bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH)
- # certificate=open(WEBHOOK_SSL_CERT, 'r'))
- if __name__ == '__main__':
- # Start flask server
- app.run(port=WEBHOOK_PORT,
- host=WEBHOOK_LISTEN,
- debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement