2017-03-23 15:34:33 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This example shows webhook echo bot with Tornado web framework
|
|
|
|
# Documenation to Tornado: http://tornadoweb.org
|
|
|
|
|
2018-08-17 12:54:26 +03:00
|
|
|
import signal
|
|
|
|
|
2017-03-23 15:34:33 +03:00
|
|
|
import tornado.httpserver
|
2018-08-17 12:54:26 +03:00
|
|
|
import tornado.ioloop
|
2017-03-23 15:34:33 +03:00
|
|
|
import tornado.options
|
2018-08-17 12:54:26 +03:00
|
|
|
import tornado.web
|
|
|
|
|
|
|
|
import telebot
|
2017-03-23 15:34:33 +03:00
|
|
|
|
|
|
|
API_TOKEN = '<api_token>'
|
|
|
|
WEBHOOK_CERT = "./cert.pem"
|
|
|
|
WEBHOOK_PKEY = "./pkey.pem"
|
|
|
|
WEBHOOK_HOST = "<domain_or_ip>"
|
|
|
|
WEBHOOK_SECRET = "<secret_uri_for_updates"
|
|
|
|
WEBHOOK_PORT = 88
|
|
|
|
WEBHOOK_URL_BASE = "https://{0}:{1}/{2}".format(WEBHOOK_HOST, str(WEBHOOK_PORT), WEBHOOK_SECRET)
|
|
|
|
|
|
|
|
# Quick'n'dirty SSL certificate generation:
|
|
|
|
#
|
|
|
|
# openssl genrsa -out pkey.pem 2048
|
|
|
|
# openssl req -new -x509 -days 3650 -key pkey.pem -out cert.pem
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
|
|
|
bot = telebot.TeleBot(API_TOKEN)
|
|
|
|
|
2018-08-17 13:01:03 +03:00
|
|
|
|
2017-03-23 15:34:33 +03:00
|
|
|
class Root(tornado.web.RequestHandler):
|
|
|
|
def get(self):
|
|
|
|
self.write("Hi! This is webhook example!")
|
|
|
|
self.finish()
|
|
|
|
|
2018-08-17 13:01:03 +03:00
|
|
|
|
2017-03-23 15:34:33 +03:00
|
|
|
class webhook_serv(tornado.web.RequestHandler):
|
|
|
|
def get(self):
|
|
|
|
self.write("What are you doing here?")
|
|
|
|
self.finish()
|
2018-08-17 13:01:03 +03:00
|
|
|
|
2017-03-23 15:34:33 +03:00
|
|
|
def post(self):
|
|
|
|
if "Content-Length" in self.request.headers and \
|
|
|
|
"Content-Type" in self.request.headers and \
|
|
|
|
self.request.headers['Content-Type'] == "application/json":
|
|
|
|
|
|
|
|
# length = int(self.request.headers['Content-Length'])
|
|
|
|
json_data = self.request.body.decode("utf-8")
|
|
|
|
update = telebot.types.Update.de_json(json_data)
|
|
|
|
bot.process_new_updates([update])
|
|
|
|
self.write("")
|
|
|
|
self.finish()
|
|
|
|
else:
|
|
|
|
self.write("What are you doing here?")
|
|
|
|
self.finish()
|
2018-08-17 13:01:03 +03:00
|
|
|
|
|
|
|
|
2017-03-23 15:34:33 +03:00
|
|
|
tornado.options.define("port", default=WEBHOOK_PORT, help="run on the given port", type=int)
|
|
|
|
is_closing = False
|
2018-08-17 13:01:03 +03:00
|
|
|
|
|
|
|
|
2017-03-23 15:34:33 +03:00
|
|
|
def signal_handler(signum, frame):
|
|
|
|
global is_closing
|
|
|
|
print("Exiting...")
|
|
|
|
is_closing = True
|
|
|
|
|
2018-08-17 13:01:03 +03:00
|
|
|
|
2017-03-23 15:34:33 +03:00
|
|
|
def try_exit():
|
|
|
|
global is_closing
|
|
|
|
if is_closing:
|
|
|
|
# clean up here
|
|
|
|
tornado.ioloop.IOLoop.instance().stop()
|
|
|
|
print("Exit success!")
|
2018-08-17 13:01:03 +03:00
|
|
|
|
|
|
|
|
2017-03-23 15:34:33 +03:00
|
|
|
# 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."))
|
|
|
|
|
2018-08-17 13:01:03 +03:00
|
|
|
|
2017-03-23 15:34:33 +03:00
|
|
|
bot.remove_webhook()
|
|
|
|
bot.set_webhook(url=WEBHOOK_URL_BASE,
|
|
|
|
certificate=open(WEBHOOK_CERT, 'r'))
|
|
|
|
tornado.options.options.logging = None
|
|
|
|
tornado.options.parse_command_line()
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
application = tornado.web.Application([
|
|
|
|
(r"/", Root),
|
|
|
|
(r"/" + WEBHOOK_SECRET, webhook_serv)
|
|
|
|
])
|
|
|
|
|
|
|
|
http_server = tornado.httpserver.HTTPServer(application, ssl_options={
|
2018-08-17 13:01:03 +03:00
|
|
|
"certfile": WEBHOOK_CERT,
|
|
|
|
"keyfile" : WEBHOOK_PKEY,
|
|
|
|
})
|
2017-03-23 15:34:33 +03:00
|
|
|
http_server.listen(tornado.options.options.port)
|
|
|
|
tornado.ioloop.PeriodicCallback(try_exit, 100).start()
|
|
|
|
tornado.ioloop.IOLoop.instance().start()
|