3131
3232bot = telebot .TeleBot (API_TOKEN )
3333
34+
3435class Root (tornado .web .RequestHandler ):
3536 def get (self ):
3637 self .write ("Hi! This is webhook example!" )
3738 self .finish ()
3839
40+
3941class webhook_serv (tornado .web .RequestHandler ):
4042 def get (self ):
4143 self .write ("What are you doing here?" )
4244 self .finish ()
45+
4346 def post (self ):
4447 if "Content-Length" in self .request .headers and \
4548 "Content-Type" in self .request .headers and \
@@ -54,28 +57,34 @@ def post(self):
5457 else :
5558 self .write ("What are you doing here?" )
5659 self .finish ()
57-
60+
61+
5862tornado .options .define ("port" , default = WEBHOOK_PORT , help = "run on the given port" , type = int )
5963is_closing = False
64+
65+
6066def signal_handler (signum , frame ):
6167 global is_closing
6268 print ("Exiting..." )
6369 is_closing = True
6470
71+
6572def try_exit ():
6673 global is_closing
6774 if is_closing :
6875 # clean up here
6976 tornado .ioloop .IOLoop .instance ().stop ()
7077 print ("Exit success!" )
71-
78+
79+
7280# Handle '/start' and '/help'
7381@bot .message_handler (commands = ['help' , 'start' ])
7482def send_welcome (message ):
7583 bot .reply_to (message ,
7684 ("Hi there, I am EchoBot.\n "
7785 "I am here to echo your kind words back to you." ))
7886
87+
7988bot .remove_webhook ()
8089bot .set_webhook (url = WEBHOOK_URL_BASE ,
8190 certificate = open (WEBHOOK_CERT , 'r' ))
@@ -88,9 +97,9 @@ def send_welcome(message):
8897])
8998
9099http_server = tornado .httpserver .HTTPServer (application , ssl_options = {
91- "certfile" : WEBHOOK_CERT ,
92- "keyfile" : WEBHOOK_PKEY ,
93- })
100+ "certfile" : WEBHOOK_CERT ,
101+ "keyfile" : WEBHOOK_PKEY ,
102+ })
94103http_server .listen (tornado .options .options .port )
95104tornado .ioloop .PeriodicCallback (try_exit , 100 ).start ()
96105tornado .ioloop .IOLoop .instance ().start ()
0 commit comments