2022-01-03 15:16:19 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This is a simple echo bot using decorators and webhook with fastapi
|
|
|
|
# It echoes any incoming text messages and does not use the polling method.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import fastapi
|
2022-08-08 23:07:04 +03:00
|
|
|
import uvicorn
|
2022-01-03 15:16:19 +03:00
|
|
|
import telebot
|
|
|
|
|
|
|
|
API_TOKEN = 'TOKEN'
|
|
|
|
|
|
|
|
WEBHOOK_HOST = '<ip/domain>'
|
|
|
|
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)
|
|
|
|
|
2022-08-08 23:04:36 +03:00
|
|
|
app = fastapi.FastAPI(docs=None, redoc_url=None)
|
2022-01-03 15:16:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
@app.post(f'/{API_TOKEN}/')
|
|
|
|
def process_webhook(update: dict):
|
2022-08-08 23:07:04 +03:00
|
|
|
"""
|
|
|
|
Process webhook calls
|
|
|
|
"""
|
2022-01-03 15:16:19 +03:00
|
|
|
if update:
|
|
|
|
update = telebot.types.Update.de_json(update)
|
|
|
|
bot.process_new_updates([update])
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
@bot.message_handler(commands=['help', 'start'])
|
|
|
|
def send_welcome(message):
|
2022-08-08 23:07:04 +03:00
|
|
|
"""
|
|
|
|
Handle '/start' and '/help'
|
|
|
|
"""
|
2022-01-03 15:16:19 +03:00
|
|
|
bot.reply_to(message,
|
|
|
|
("Hi there, I am EchoBot.\n"
|
|
|
|
"I am here to echo your kind words back to you."))
|
|
|
|
|
|
|
|
|
|
|
|
@bot.message_handler(func=lambda message: True, content_types=['text'])
|
|
|
|
def echo_message(message):
|
2022-08-08 23:07:04 +03:00
|
|
|
"""
|
|
|
|
Handle all other messages
|
|
|
|
"""
|
2022-01-03 15:16:19 +03:00
|
|
|
bot.reply_to(message, message.text)
|
|
|
|
|
|
|
|
|
|
|
|
# Remove webhook, it fails sometimes the set if there is a previous webhook
|
|
|
|
bot.remove_webhook()
|
|
|
|
|
|
|
|
# Set webhook
|
2022-08-08 23:07:04 +03:00
|
|
|
bot.set_webhook(
|
|
|
|
url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
|
|
|
|
certificate=open(WEBHOOK_SSL_CERT, 'r')
|
|
|
|
)
|
2022-01-03 15:16:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
uvicorn.run(
|
|
|
|
app,
|
|
|
|
host=WEBHOOK_LISTEN,
|
|
|
|
port=WEBHOOK_PORT,
|
|
|
|
ssl_certfile=WEBHOOK_SSL_CERT,
|
|
|
|
ssl_keyfile=WEBHOOK_SSL_PRIV
|
|
|
|
)
|