0

This is the code:

AttributeError: 'NoneType' object has no attribute 'bot'

from telegram.ext import CallbackContext
from telegram import Bot
import os

# Initialize your bot with the token from environment variable
bot_token = os.getenv('BOT_TOKEN')
if not bot_token:
    raise RuntimeError('BOT_TOKEN environment variable is not set.')

bot = Bot(bot_token)

# Messages for notifications
START_WORKDAY_MESSAGE_DRIVERS = "🚗 Work day: Notification system started! Get ready for a productive day ahead. 🌟"
END_WORKDAY_MESSAGE_DRIVERS = "🌙 Job ended for today. Thank you for your hard work! See you tomorrow. 👋"

START_WORKDAY_MESSAGE_STUDENTS = "🚌 Shuttle service is now available! You can start requesting rides. 🌟"
END_WORKDAY_MESSAGE_STUDENTS = "🚌 Shuttle service has ended for today. See you again tomorrow! 👋"

# Group ID intentionally removed
drivers_group_chat_id = -xxxxxxxxxx
pwd_group_chat_id = -xxxxxxxxxx

async def notify_workday_start_drivers(context: CallbackContext) -> None:
    await context.bot.send_message(drivers_group_chat_id, START_WORKDAY_MESSAGE_DRIVERS)

async def notify_workday_end_drivers(context: CallbackContext) -> None:
    await context.bot.send_message(drivers_group_chat_id, END_WORKDAY_MESSAGE_DRIVERS)

async def notify_workday_start_students(context: CallbackContext) -> None:
    await context.bot.send_message(pwd_group_chat_id, START_WORKDAY_MESSAGE_STUDENTS)

async def notify_workday_end_students(context: CallbackContext) -> None:
    await context.bot.send_message(pwd_group_chat_id, END_WORKDAY_MESSAGE_STUDENTS)

I've deployed the bot to Heroku and when it gets to send the message I get the following error in the Heroku logs:

2024-06-16T23:07:00.043132+00:00 app[web.1]: Traceback (most recent call last):
2024-06-16T23:07:00.043133+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/apscheduler/executors/base_py3.py", line 30, in run_coroutine_job
2024-06-16T23:07:00.043133+00:00 app[web.1]: retval = await job.func(*job.args, **job.kwargs)
2024-06-16T23:07:00.043134+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-06-16T23:07:00.043134+00:00 app[web.1]: File "/app/notifications.py", line 28, in notify_workday_end_drivers
2024-06-16T23:07:00.043135+00:00 app[web.1]: await context.bot.send_message(drivers_group_chat_id, END_WORKDAY_MESSAGE_DRIVERS)
2024-06-16T23:07:00.043135+00:00 app[web.1]: ^^^^^^^^^^^
2024-06-16T23:07:00.043135+00:00 app[web.1]: AttributeError: 'NoneType' object has no attribute 'bot'
2024-06-16T23:07:00.043268+00:00 app[web.1]: 2024-06-16 23:07:00,043 - apscheduler.executors.default - INFO - Running job "notify_workday_end_students (trigger: cron[hour='23', minute='7'], next run at: 2024-06-17 23:07:00 UTC)" (scheduled at 2024-06-16 23:07:00+00:00)
2024-06-16T23:07:00.043293+00:00 app[web.1]: Context object: None

2024-06-16T23:07:00.043772+00:00 app[web.1]: Traceback (most recent call last):
2024-06-16T23:07:00.043772+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/apscheduler/executors/base_py3.py", line 30, in run_coroutine_job
2024-06-16T23:07:00.043772+00:00 app[web.1]: retval = await job.func(*job.args, **job.kwargs)
2024-06-16T23:07:00.043773+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-06-16T23:07:00.043773+00:00 app[web.1]: File "/app/notifications.py", line 36, in notify_workday_end_students
2024-06-16T23:07:00.043773+00:00 app[web.1]: await context.bot.send_message(pwd_group_chat_id, END_WORKDAY_MESSAGE_STUDENTS)
2024-06-16T23:07:00.043773+00:00 app[web.1]: ^^^^^^^^^^^
2024-06-16T23:07:00.043774+00:00 app[web.1]: AttributeError: 'NoneType' object has no attribute 'bot'
3
  • 1
    you may want to go read this page about how to write your first bot. you are missing a bunch of setup code, etc. github.com/python-telegram-bot/python-telegram-bot/wiki/… Commented Jun 17, 2024 at 0:26
  • Somehow, the notify_workday_end_drivers() and notify_workday_end_students() functions were called with a null context argument. We can't say any more than that, because you have not shown us how those functions are called. Commented Jun 17, 2024 at 0:27
  • @JohnGordon, I call them using apscheduler scheduler.add_job( notifications.notify_workday_end_students, trigger='cron', hour=00, # Adjust the hour as per your requirement (e.g., 5 PM) minute=35, # Adjust the minute as per your requirement id='workday_end_notification_students' ) Commented Jun 17, 2024 at 0:40

1 Answer 1

1
async def notify_workday_start_drivers() -> None:
await bot.send_message(drivers_group_chat_id, START_WORKDAY_MESSAGE_DRIVERS)

I modified the functions to directly use the bot global variable instead of context. This removes the need for context as an argument and with this the problem was solved

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.