Last active
September 13, 2021 00:03
-
-
Save Denver-sn/876243d4e7fe49e1b4420116942c2e7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
bind = "unix:/run/app.sock" #Socket su cui deve ascoltare gunicorn | |
threads = multiprocessing.cpu_count() * 2 + 1 | |
wsgi_app = "start:app" #Modulo contenente la flask app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import time | |
import flask | |
import telebot | |
API_TOKEN = 'MY_TOKEN' | |
WEBHOOK_HOST = 'antares-foundation.xyz' | |
WEBHOOK_PORT = 80 # 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) | |
app = flask.Flask(__name__) | |
# Empty webserver index, return nothing, just http 200 | |
@app.route('/', methods=['GET', 'HEAD']) | |
def index(): | |
return '' | |
# Process webhook calls | |
@app.route(WEBHOOK_URL_PATH, methods=['POST']) | |
def webhook(): | |
if flask.request.headers.get('content-type') == 'application/json': | |
json_string = flask.request.get_data().decode('utf-8') | |
update = telebot.types.Update.de_json(json_string) | |
bot.process_new_updates([update]) | |
return '' | |
else: | |
flask.abort(403) | |
# 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() | |
time.sleep(0.1) | |
# Set webhook | |
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH, | |
certificate=open(WEBHOOK_SSL_CERT, 'r')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment