|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# This is a simple echo bot using decorators and webhook with CherryPy |
| 5 | +# It echoes any incoming text messages and does not use the polling method. |
| 6 | + |
| 7 | +import cherrypy |
| 8 | +import telebot |
| 9 | +import logging |
| 10 | + |
| 11 | + |
| 12 | +API_TOKEN = '<api_token>' |
| 13 | + |
| 14 | +WEBHOOK_HOST = '<ip/host where the bot is running>' |
| 15 | +WEBHOOK_PORT = 8443 # 443, 80, 88 or 8443 (port need to be 'open') |
| 16 | +WEBHOOK_LISTEN = '0.0.0.0' # In some VPS you may need to put here the IP addr |
| 17 | + |
| 18 | +WEBHOOK_SSL_CERT = './webhook_cert.pem' # Path to the ssl certificate |
| 19 | +WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key |
| 20 | + |
| 21 | +# Quick'n'dirty SSL certificate generation: |
| 22 | +# |
| 23 | +# openssl genrsa -out webhook_pkey.pem 2048 |
| 24 | +# openssl req -new -x509 -days 3650 -key webhook_pkey.pem -out webhook_cert.pem |
| 25 | +# |
| 26 | +# When asked for "Common Name (e.g. server FQDN or YOUR name)" you should reply |
| 27 | +# with the same value in you put in WEBHOOK_HOST |
| 28 | + |
| 29 | +WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT) |
| 30 | +WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN) |
| 31 | + |
| 32 | + |
| 33 | +logger = telebot.logger |
| 34 | +telebot.logger.setLevel(logging.INFO) |
| 35 | + |
| 36 | +bot = telebot.TeleBot(API_TOKEN) |
| 37 | + |
| 38 | + |
| 39 | +# WebhookServer, process webhook calls |
| 40 | +class WebhookServer(object): |
| 41 | + @cherrypy.expose |
| 42 | + def index(self): |
| 43 | + if 'content-length' in cherrypy.request.headers and \ |
| 44 | + 'content-type' in cherrypy.request.headers and \ |
| 45 | + cherrypy.request.headers['content-type'] == 'application/json': |
| 46 | + length = int(cherrypy.request.headers['content-length']) |
| 47 | + json_string = cherrypy.request.body.read(length) |
| 48 | + update = telebot.types.Update.de_json(json_string) |
| 49 | + bot.process_new_messages([update.message]) |
| 50 | + return '' |
| 51 | + else: |
| 52 | + raise cherrypy.HTTPError(403) |
| 53 | + |
| 54 | + |
| 55 | +# Handle '/start' and '/help' |
| 56 | +@bot.message_handler(commands=['help', 'start']) |
| 57 | +def send_welcome(message): |
| 58 | + bot.reply_to(message, |
| 59 | + ("Hi there, I am EchoBot.\n" |
| 60 | + "I am here to echo your kind words back to you.")) |
| 61 | + |
| 62 | + |
| 63 | +# Handle all other messages |
| 64 | +@bot.message_handler(func=lambda message: True, content_types=['text']) |
| 65 | +def echo_message(message): |
| 66 | + bot.reply_to(message, message.text) |
| 67 | + |
| 68 | + |
| 69 | +# Remove webhook, it fails sometimes the set if there is a previous webhook |
| 70 | +bot.remove_webhook() |
| 71 | + |
| 72 | +# Set webhook |
| 73 | +bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH, |
| 74 | + certificate=open(WEBHOOK_SSL_CERT, 'r')) |
| 75 | + |
| 76 | +# Start cherrypy server |
| 77 | +cherrypy.config.update({ |
| 78 | + 'server.socket_host': WEBHOOK_LISTEN, |
| 79 | + 'server.socket_port': WEBHOOK_PORT, |
| 80 | + 'server.ssl_module': 'builtin', |
| 81 | + 'server.ssl_certificate': WEBHOOK_SSL_CERT, |
| 82 | + 'server.ssl_private_key': WEBHOOK_SSL_PRIV |
| 83 | +}) |
| 84 | + |
| 85 | +cherrypy.quickstart(WebhookServer(), WEBHOOK_URL_PATH, {'/': {}}) |
0 commit comments