Heli
Heli

Reputation: 1

Python Telegram bot freezes when run by Airflow

I'm trying to execute a Python Telegram Bot through an Airflow DAG. The DAG works and execute the bot, but it don't do any response to the commands.

When I execute the file manually it do the responses normally.

I'm using Astro CLI 1.31.0 to run Airflow in a docker container. I'm new using Airflow.

Here is my code, I've tried to use NO_PROXY = "*" but I get no result.

import telebot
import os
from dags.manut.Consulta_Geoex_DAG_Bib import testa_cookie, abre, escreve_json
from airflow.api.client.local_client import Client

os.environ\['NO_PROXY'\] = '\*'

API_TOKEN = 'TOKEN VALUE'
bot = telebot.TeleBot(API_TOKEN)

c = Client(None, None)

cookie, gxsessao = '',''
data = abre('dags/manut/\_internal/cookie.json')

def run_bot(bot=bot):
print("Iniciando bot")

    @bot.message_handler(commands=['help', 'start'])
    def send_welcome(message):
        bot.send_message(message.chat.id, """Bot para atualizar o status das Informações do Geoex.
        """)
    
    @bot.message_handler(commands=['cookie'])
    def send_cookie(message):
        msg = bot.send_message(message.chat.id, '''
                        Atualizando informações de acesso ao Geoex.
                        Insira o Cookie:
                        ''')
        bot.register_next_step_handler(msg,ler_cookie)
    
    def ler_cookie(message):
        global cookie
        cookie = message.text
        msg = bot.reply_to(message, 'Insira Gxsessao:')
        bot.register_next_step_handler(msg, ler_gxsessao)
    
    def ler_gxsessao(message):
        global gxsessao, data
        gxsessao = message.text
        cookie_valido = testa_cookie(cookie=cookie, gxsessao=gxsessao)
        if cookie_valido:
            data['cookie']=cookie
            data['gxsessao']=gxsessao
            escreve_json('dags/manut/_internal/cookie.json',data)
            c.trigger_dag(dag_id='cookie-manut')
            msg = 'Informações atualizadas com sucesso!'
        else:
            msg = 'Dados inválidos.'
        bot.reply_to(message, msg)
    
    @bot.message_handler(commands=['pastas'])
    def pastas(message):
        c.trigger_dag(dag_id='pastas-projetos-controle')
        msg = 'Atualizando status das pastas.'
        bot.reply_to(message, msg)
    
    @bot.message_handler(func=lambda message: True)
    def echo_message(message):
        bot.reply_to(message, 'Comando Inválido.')
    
    bot.infinity_polling()

if __name__ == '__main__':
run_bot()

Edit: I fixed it by adding bot = telebot.TeleBot(API_TOKEN, threaded=False) . No idea why it works after all, maybe was some conflict with airflow's threading.

Upvotes: 0

Views: 15

Answers (0)

Related Questions