From 6c770d81f96d75cd81c83d6a6edbd6d050b8020a Mon Sep 17 00:00:00 2001 From: robox Date: Mon, 30 Jan 2017 01:54:49 +0600 Subject: [PATCH] Extend examples with aiohttp --- examples/webhook_examples/README.md | 13 ++- .../webhook_aiohttp_echo_bot.py | 88 +++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 examples/webhook_examples/webhook_aiohttp_echo_bot.py diff --git a/examples/webhook_examples/README.md b/examples/webhook_examples/README.md index 2d47346..b1e4351 100644 --- a/examples/webhook_examples/README.md +++ b/examples/webhook_examples/README.md @@ -1,6 +1,6 @@ # Webhook examples using pyTelegramBotAPI -There are 3 examples in this directory using different libraries: +There are 4 examples in this directory using different libraries: * **Python (CPython):** *webhook_cpython_echo_bot.py* * **Pros:** @@ -32,5 +32,14 @@ There are 3 examples in this directory using different libraries: * The project seems not to be very active, latest version dates 2013. * They don't recommend to use it with Python 3, but may work. * May be a oversized for just handling webhook petitions. + +* **aiohttp (1.2.0):** *webhook_aiohttp_echo_bot.py* + * **Pros:** + * It's a web application framework + * Python 3 compatible + * Asynchronous, excellent perfomance + * Utilizes new async/await syntax + * **Cons:** + * Requires Python 3.4.2+, don't work with Python 2 -*Latest update of this document: 2015-10-06* +*Latest update of this document: 2017-01-30* diff --git a/examples/webhook_examples/webhook_aiohttp_echo_bot.py b/examples/webhook_examples/webhook_aiohttp_echo_bot.py new file mode 100644 index 0000000..d92cff9 --- /dev/null +++ b/examples/webhook_examples/webhook_aiohttp_echo_bot.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# This is a simple echo bot using decorators and webhook with aiohttp +# It echoes any incoming text messages and does not use the polling method. + +import logging +import ssl + +from aiohttp import web + +import telebot + + +API_TOKEN = '' + +WEBHOOK_HOST = '' +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) + +app = web.Application() + + +# Process webhook calls +async def handle(request): + if request.match_info.get('token') == bot.token: + request_body_dict = await request.json() + update = telebot.types.Update.de_json(request_body_dict) + bot.process_new_updates([update]) + return web.Response() + else: + return web.Response(status=403) + +app.router.add_post('/{token}/', handle) + + +# 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')) + +# Build ssl context +context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) +context.load_cert_chain(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV) + +# Start aiohttp server +web.run_app( + app, + host=WEBHOOK_LISTEN, + port=WEBHOOK_PORT, + ssl_context=context, +)