Uncategorized

django – Why python-telegram-bot is not working with celery?


I tried to use python-telegram-bot but it was not working while when I tried to use it out of celery asyncio with asds it was working well
What is wrong with this code runing python-telegram-bot with celery on my django app:
asyncio:


async def send_telegram_message(message):
    from telegram import Bot

    bot = Bot(token=TELEGRAM_BOT_TOKEN)
    try:
        response = await bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=message)
        print(f"Message sent successfully. Message ID: {response.message_id}")
    except Exception as e:
        print(f"Messageeeeeeeeee ID: {e}")


# @shared_task
async def check_deadline():
    from telegram import Bot


    boards_url = f'https://api.trello.com/1/members/me/boards?key={TRELLO_API_KEY}&token={TRELLO_API_TOKEN}'
    boards_response = requests.get(boards_url)

    cards_url = f'https://api.trello.com/1/boards/6593cb5ef822ce42ea6645cd/cards?key={TRELLO_API_KEY}&token={TRELLO_API_TOKEN}'
    cards_response = requests.get(cards_url)
    cards = cards_response.json()
    await send_telegram_message('test')

if __name__ == "__main__":
    import asyncio
    loop = asyncio.get_event_loop()
    loop.run_until_complete(check_deadline())

celery:

def send_telegram_message(message):
    bot = Bot(token=TELEGRAM_BOT_TOKEN)
    try:
        response = bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=message)
        print(f"Message sent successfully. Message ID: {response.message_id}")
    except Exception as e:
        print(f"Message ID: {e}")


@shared_task
def check_deadline():
    print('check_deadline task started!')
    boards_url = f'https://api.trello.com/1/members/me/boards?key={TRELLO_API_KEY}&token={TRELLO_API_TOKEN}'
    boards_response = requests.get(boards_url)

    cards_url = f'https://api.trello.com/1/boards/[baordId]/cards?key={TRELLO_API_KEY}&token={TRELLO_API_TOKEN}'
    cards_response = requests.get(cards_url)
    cards = cards_response.json()

    send_telegram_message('asdsadasd')

I was expecting bot to send message correctly with the celery beat?



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *