2015-10-06 13:06:23 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This is a simple echo bot using decorators and webhook with BaseHTTPServer
|
|
|
|
# It echoes any incoming text messages and does not use the polling method.
|
|
|
|
|
2018-08-17 12:47:44 +03:00
|
|
|
try:
|
|
|
|
# Python 2
|
|
|
|
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
except ImportError:
|
|
|
|
# Python 3
|
|
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
|
2015-10-06 13:06:23 +03:00
|
|
|
import logging
|
2018-08-17 12:54:26 +03:00
|
|
|
import ssl
|
2015-10-06 13:06:23 +03:00
|
|
|
|
2018-08-17 12:54:26 +03:00
|
|
|
import telebot
|
2015-10-06 13:06:23 +03:00
|
|
|
|
|
|
|
API_TOKEN = '<api_token>'
|
|
|
|
|
|
|
|
WEBHOOK_HOST = '<ip/host where the bot is running>'
|
|
|
|
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
|
|
|
|
|
|
|
|
WEBHOOK_SSL_CERT = './webhook_cert.pem' # Path to the ssl certificate
|
|
|
|
WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key
|
|
|
|
|
|
|
|
# Quick'n'dirty SSL certificate generation:
|
|
|
|
#
|
|
|
|
# openssl genrsa -out webhook_pkey.pem 2048
|
|
|
|
# openssl req -new -x509 -days 3650 -key webhook_pkey.pem -out webhook_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
|
|
|
|
|
|
|
|
WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
|
|
|
|
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
|
|
|
|
|
|
|
|
logger = telebot.logger
|
|
|
|
telebot.logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
bot = telebot.TeleBot(API_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
# WebhookHandler, process webhook calls
|
2018-08-17 12:47:44 +03:00
|
|
|
class WebhookHandler(BaseHTTPRequestHandler):
|
2015-10-06 13:06:23 +03:00
|
|
|
server_version = "WebhookHandler/1.0"
|
|
|
|
|
|
|
|
def do_HEAD(self):
|
|
|
|
self.send_response(200)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
self.send_response(200)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
if self.path == WEBHOOK_URL_PATH and \
|
|
|
|
'content-type' in self.headers and \
|
|
|
|
'content-length' in self.headers and \
|
|
|
|
self.headers['content-type'] == 'application/json':
|
|
|
|
json_string = self.rfile.read(int(self.headers['content-length']))
|
|
|
|
|
|
|
|
self.send_response(200)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
update = telebot.types.Update.de_json(json_string)
|
|
|
|
bot.process_new_messages([update.message])
|
|
|
|
else:
|
|
|
|
self.send_error(403)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
2018-12-29 18:49:10 +03:00
|
|
|
#bot.remove_webhook()
|
2015-10-06 13:06:23 +03:00
|
|
|
|
|
|
|
# Set webhook
|
2018-12-29 18:49:10 +03:00
|
|
|
# Beacuse telegram bot api server will check webhook server is alive.
|
|
|
|
# Here we need set webhook after server started manually.
|
|
|
|
#bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
|
|
|
|
# certificate=open(WEBHOOK_SSL_CERT, 'r'))
|
2015-10-06 13:06:23 +03:00
|
|
|
|
|
|
|
# Start server
|
2018-08-17 12:47:44 +03:00
|
|
|
httpd = HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT),
|
2018-08-17 13:01:03 +03:00
|
|
|
WebhookHandler)
|
2015-10-06 13:06:23 +03:00
|
|
|
|
|
|
|
httpd.socket = ssl.wrap_socket(httpd.socket,
|
|
|
|
certfile=WEBHOOK_SSL_CERT,
|
|
|
|
keyfile=WEBHOOK_SSL_PRIV,
|
|
|
|
server_side=True)
|
|
|
|
|
|
|
|
httpd.serve_forever()
|