|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# This example shows webhook echo bot with Tornado web framework |
| 5 | +# Documenation to Tornado: http://tornadoweb.org |
| 6 | + |
| 7 | +import telebot |
| 8 | +import tornado.web |
| 9 | +import tornado.ioloop |
| 10 | +import tornado.httpserver |
| 11 | +import tornado.options |
| 12 | +import signal |
| 13 | + |
| 14 | +API_TOKEN = '<api_token>' |
| 15 | +WEBHOOK_CERT = "./cert.pem" |
| 16 | +WEBHOOK_PKEY = "./pkey.pem" |
| 17 | +WEBHOOK_HOST = "<domain_or_ip>" |
| 18 | +WEBHOOK_SECRET = "<secret_uri_for_updates" |
| 19 | +WEBHOOK_PORT = 88 |
| 20 | +WEBHOOK_URL_BASE = "https://{0}:{1}/{2}".format(WEBHOOK_HOST, str(WEBHOOK_PORT), WEBHOOK_SECRET) |
| 21 | + |
| 22 | +# Quick'n'dirty SSL certificate generation: |
| 23 | +# |
| 24 | +# openssl genrsa -out pkey.pem 2048 |
| 25 | +# openssl req -new -x509 -days 3650 -key pkey.pem -out cert.pem |
| 26 | +# |
| 27 | +# When asked for "Common Name (e.g. server FQDN or YOUR name)" you should reply |
| 28 | +# with the same value in you put in WEBHOOK_HOST |
| 29 | + |
| 30 | +bot = telebot.TeleBot(API_TOKEN) |
| 31 | + |
| 32 | +class Root(tornado.web.RequestHandler): |
| 33 | + def get(self): |
| 34 | + self.write("Hi! This is webhook example!") |
| 35 | + self.finish() |
| 36 | + |
| 37 | +class webhook_serv(tornado.web.RequestHandler): |
| 38 | + def get(self): |
| 39 | + self.write("What are you doing here?") |
| 40 | + self.finish() |
| 41 | + def post(self): |
| 42 | + if "Content-Length" in self.request.headers and \ |
| 43 | + "Content-Type" in self.request.headers and \ |
| 44 | + self.request.headers['Content-Type'] == "application/json": |
| 45 | + |
| 46 | + # length = int(self.request.headers['Content-Length']) |
| 47 | + json_data = self.request.body.decode("utf-8") |
| 48 | + update = telebot.types.Update.de_json(json_data) |
| 49 | + bot.process_new_updates([update]) |
| 50 | + self.write("") |
| 51 | + self.finish() |
| 52 | + else: |
| 53 | + self.write("What are you doing here?") |
| 54 | + self.finish() |
| 55 | + |
| 56 | +tornado.options.define("port", default=WEBHOOK_PORT, help="run on the given port", type=int) |
| 57 | +is_closing = False |
| 58 | +def signal_handler(signum, frame): |
| 59 | + global is_closing |
| 60 | + print("Exiting...") |
| 61 | + is_closing = True |
| 62 | + |
| 63 | +def try_exit(): |
| 64 | + global is_closing |
| 65 | + if is_closing: |
| 66 | + # clean up here |
| 67 | + tornado.ioloop.IOLoop.instance().stop() |
| 68 | + print("Exit success!") |
| 69 | + |
| 70 | +# Handle '/start' and '/help' |
| 71 | +@bot.message_handler(commands=['help', 'start']) |
| 72 | +def send_welcome(message): |
| 73 | + bot.reply_to(message, |
| 74 | + ("Hi there, I am EchoBot.\n" |
| 75 | + "I am here to echo your kind words back to you.")) |
| 76 | + |
| 77 | +bot.remove_webhook() |
| 78 | +bot.set_webhook(url=WEBHOOK_URL_BASE, |
| 79 | + certificate=open(WEBHOOK_CERT, 'r')) |
| 80 | +tornado.options.options.logging = None |
| 81 | +tornado.options.parse_command_line() |
| 82 | +signal.signal(signal.SIGINT, signal_handler) |
| 83 | +application = tornado.web.Application([ |
| 84 | + (r"/", Root), |
| 85 | + (r"/" + WEBHOOK_SECRET, webhook_serv) |
| 86 | +]) |
| 87 | + |
| 88 | +http_server = tornado.httpserver.HTTPServer(application, ssl_options={ |
| 89 | + "certfile": WEBHOOK_CERT, |
| 90 | + "keyfile": WEBHOOK_PKEY, |
| 91 | + }) |
| 92 | +http_server.listen(tornado.options.options.port) |
| 93 | +tornado.ioloop.PeriodicCallback(try_exit, 100).start() |
| 94 | +tornado.ioloop.IOLoop.instance().start() |
0 commit comments