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.
This error is likely occurring because the send_message method is not available on the Update object. The Update object is a class provided by the python-telegram-bot library that represents an incoming update from the Telegram API. It does not have a send_message method because it is not responsible for sending messages - it is only used to represent updates received from the API.
To send a message with a Telegram bot, you will need to use the Bot object provided by the python-telegram-bot library. The Bot object has a send_message method that you can use to send a message to a user or group.
Here's an example of how to use the send_message method:
import telegram
bot = telegram.Bot(token='YOUR_BOT_TOKEN')
chat_id = 12345678 # Replace with the chat ID of the recipient
text = 'Hello, World!'
bot.send_message(chat_id=chat_id, text=text)
This code will send the message Hello, World. to the chat with the ID 12345678.
You will need to obtain a bot token from the Telegram Botfather and replace YOUR_BOT_TOKEN with your actual bot token in order for this code to work.
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, update):
bot.send_message(chat_id=update.message.chat_id, text="Hello")
start_handler = CommandHandler("start", start)
dispatcher.add_handler(start_handler)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.