1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge pull request #1043 from vixfwis/master

Add webhook example for Twisted
This commit is contained in:
Badiboy 2020-12-17 16:49:26 +03:00 committed by GitHub
commit b522053e27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 2 deletions

View File

@ -1,6 +1,6 @@
# Webhook examples using pyTelegramBotAPI
There are 4 examples in this directory using different libraries:
There are 5 examples in this directory using different libraries:
* **Python (CPython):** *webhook_cpython_echo_bot.py*
* **Pros:**
@ -42,4 +42,13 @@ There are 4 examples in this directory using different libraries:
* **Cons:**
* Requires Python 3.4.2+, don't work with Python 2
*Latest update of this document: 2017-01-30*
* **Twisted (20.3.0):** *webhook_twisted_echo_bot.py*
* **Pros:**
* Asynchronous event-driven networking engine
* Very high performance
* Built-in support for many internet protocols
* **Cons:**
* Twisted is low-level, which may be good or bad depending on use case
* Considerable learning curve - reading docs is a must.
*Latest update of this document: 2020-12-17*

View File

@ -0,0 +1,79 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This is an example echo bot using webhook with Twisted network framework.
# Updates are received with Twisted web server and processed in reactor thread pool.
# Relevant docs:
# https://twistedmatrix.com/documents/current/core/howto/reactor-basics.html
# https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html
import logging
import telebot
import json
from twisted.internet import ssl, reactor
from twisted.web.resource import Resource, ErrorPage
from twisted.web.server import Site, Request
API_TOKEN = '<api_token>'
WEBHOOK_HOST = '<ip or hostname>'
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://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN)
logger = telebot.logger
telebot.logger.setLevel(logging.INFO)
bot = telebot.TeleBot(API_TOKEN)
# 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
bot.remove_webhook()
# Set webhook
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
certificate=open(WEBHOOK_SSL_CERT, 'r'))
# Process webhook calls
class WebhookHandler(Resource):
isLeaf = True
def render_POST(self, request: Request):
request_body_dict = json.load(request.content)
update = telebot.types.Update.de_json(request_body_dict)
reactor.callInThread(lambda: bot.process_new_updates([update]))
return b''
root = ErrorPage(403, 'Forbidden', '')
root.putChild(API_TOKEN.encode(), WebhookHandler())
site = Site(root)
sslcontext = ssl.DefaultOpenSSLContextFactory(WEBHOOK_SSL_PRIV, WEBHOOK_SSL_CERT)
reactor.listenSSL(8443, site, sslcontext)
reactor.run()