Getting an AttributeError: 'Update' object has no attribute 'send_message' when I try to send a message with Telegram bot
Question:
Im getting an AttributeError: ‘Update’ object has no attribute ‘send_message’ when I try to send a message.
And I know that ‘Update’ class does not have a ‘send_message’ attribute but Im not using the ‘Update’ class.
Im using the ‘bot’ class but Im still getting the same exact error.
Here is my code:
from telegram.ext import Updater, CommandHandler
from telegram import Bot
bot = Bot(token='TOKEN')
def start(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Hello")
updater = Updater(token='TOKEN')
dispatcher = updater.dispatcher
command_handler = CommandHandler('start', start)
dispatcher.add_handler(command_handler)
updater.start_polling()
Here is the error;
bot.send_message(chat_id=update.message.chat_id, text="Hello")
AttributeError: 'Update' object has no attribute 'send_message'
Tried to import the Bot library separately but still nothing worked.
Answers:
I finally got it working! You need to add a context
parameter instead of an update
parameter
Here is the updated code:
def start(bot, context):
context.bot.send_message(chat_id=update.message.chat_id, text="Hello")
start_handler = CommandHandler("start", start)
dispatcher.add_handler(start_handler)
Im getting an AttributeError: ‘Update’ object has no attribute ‘send_message’ when I try to send a message.
And I know that ‘Update’ class does not have a ‘send_message’ attribute but Im not using the ‘Update’ class.
Im using the ‘bot’ class but Im still getting the same exact error.
Here is my code:
from telegram.ext import Updater, CommandHandler
from telegram import Bot
bot = Bot(token='TOKEN')
def start(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Hello")
updater = Updater(token='TOKEN')
dispatcher = updater.dispatcher
command_handler = CommandHandler('start', start)
dispatcher.add_handler(command_handler)
updater.start_polling()
Here is the error;
bot.send_message(chat_id=update.message.chat_id, text="Hello")
AttributeError: 'Update' object has no attribute 'send_message'
Tried to import the Bot library separately but still nothing worked.
I finally got it working! You need to add a context
parameter instead of an update
parameter
Here is the updated code:
def start(bot, context):
context.bot.send_message(chat_id=update.message.chat_id, text="Hello")
start_handler = CommandHandler("start", start)
dispatcher.add_handler(start_handler)