Flask + Nginx + uwsgi + telebot
Есть бот на веб-хуках, пытаюсь его запустить в прод, но чёт не выходит. Снизу будут все конфиги, и код, буду признателен если поможете)
Код самого бота!
bot = telebot.TeleBot(API_TOKEN)
app = flask.Flask(__name__)
def get_info_into_DB(path):
with open(path, 'rb') as f:
data = pickle.load(f)
return data
# 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','GET'])
def webhook():
global post_info
global post_id
if flask.request.headers.get('content-type') == 'application/json':
json_string = flask.request.get_json()
#print(json_string)
if 'message' in json_string.keys():
if 'entities' in json_string['message'].keys():
post_id = json_string['message']['text'].split(' ')[-1]
if post_id.split('_')[0] == 'continuation':
post_info = get_info_into_DB('/home/nikita/bots/message_bot/DB/post_with_continuation.pickle').get(f'{post_id.split("_")[1]}_{post_id.split("_")[2]}_{post_id.split("_")[3]}')
elif post_id.split('_')[0] == 'riddle':
post_info = get_info_into_DB('/home/nikita/bots/message_bot/DB/post_with_riddle.pickle').get(f'{post_id.split("_")[1]}_{post_id.split("_")[2]}_{post_id.split("_")[3]}')
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):
global post_info
global post_id
bot.send_message(message.from_user.id,'OK')
print(post_info)
print(post_id)
print(bot.get_chat_member(post_id.split("_")[2], message.from_user.id).status)
if post_id.split('_')[0] == 'continuation' and bot.get_chat_member(post_id.split("_")[2], message.from_user.id).status != 'left':
bot.send_photo(message.from_user.id, open(post_info[0], 'rb'), caption=f'{post_info[2]}')
else:
keyBoard_post = telebot.types.InlineKeyboardMarkup(True)
keyBoard_post.add(telebot.types.InlineKeyboardButton(text='Подписаться', url = post_info[-1]))
bot.send_message(message.from_user.id,'Для просмотра поста!\nПодпишитесь на канал!', reply_markup = keyBoard_post)
post_info = []
post_id = ''
@bot.callback_query_handler(func = lambda call:True)
def call_answer(call):
print()
bot.send_message(call.from_user.id,call.data)
# 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='https://37.230.117.88/bot')
# Start flask server
app.run(host='127.0.0.1',
port=7771)
Код uwsg
from post_bot import app
if __name__ == "__main__":
app.run()
Nginx конфиг
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name 37.230.117.88;
ssl_certificate sites-available/rootCA.pem;
ssl_certificate_key sites-available/rootCA.key;
location /bot/ {
proxy_pass http://127.0.0.1:7771/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
Ответы (1 шт):
Автор решения: Никита Швора
→ Ссылка
Разобрался, следовало указать путь к сертификату вот тут:
bot.set_webhook(url='https://37.230.117.88/bot')
Так как у меня самоподписной сертификат, это обязательно)