S
S
Slava Zudvin2020-07-08 13:16:40
Python
Slava Zudvin, 2020-07-08 13:16:40

Python Telegram Bot - how to fix Update' object has no attribute 'send_message'?

Hello!

Onboard Python by pyenv == 3.7.7

Using python-telegram-bot

| => python -m telegram
python-telegram-bot 12.8 (v12.8-2-g9288e4f)
certifi 2020.06.20
Python 3.7.7 (default, Jul  7 2020, 21:25:17)  [Clang 10.0.1 (clang-1001.0.46.4)]


There is a code from an article on Habré that I updated for the state of 2020 (the code was written in 2018):

Article on Habré: https://habr.com/ru/post/346606/

The code itself:

# Настройки
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import apiai, json
updater = Updater('Telegram API', use_context=True) # Токен API к Telegram
dispatcher = updater.dispatcher
# Обработка команд
def startCommand(bot, update):
    bot.send_message(chat_id=chat_id, text='Привет, давай пообщаемся?')
def textMessage(bot, update):
    request = apiai.ApiAI('Dialogflow API').text_request() # Токен API к Dialogflow
    request.lang = 'ru' # На каком языке будет послан запрос
    request.session_id = 'YouBot' # ID Сессии диалога (нужно, чтобы потом учить бота)
    request.query = update.message.reply_text # Посылаем запрос к ИИ с сообщением от юзера
    responseJson = json.loads(request.getresponse().read().decode('utf-8'))
    response = responseJson['result']['fulfillment']['speech'] # Разбираем JSON и вытаскиваем ответ
    # Если есть ответ от бота - присылаем юзеру, если нет - бот его не понял
    if response:
        bot.send_message(chat_id=chat_id, text=response)
    else:
        bot.send_message(chat_id=chat_id, text='Я Вас не совсем понял!')
# Хендлеры
start_command_handler = CommandHandler('start', startCommand)
text_message_handler = MessageHandler(Filters.text, textMessage)
# Добавляем хендлеры в диспетчер
dispatcher.add_handler(start_command_handler)
dispatcher.add_handler(text_message_handler)
# Начинаем поиск обновлений
updater.start_polling(clean=True)
# Останавливаем бота, если были нажаты Ctrl + C
updater.idle()


The error that the console throws:

python bot.py
No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "/Users/project-python/hhbot/python-telegram-bot/telegram/ext/dispatcher.py", line 340, in process_update
    handler.handle_update(update, self, check, context)
  File "/Users/project-python/hhbot/python-telegram-bot/telegram/ext/handler.py", line 119, in handle_update
    return self.callback(update, context)
  File "bot.py", line 8, in startCommand
    bot.send_message(chat_id=chat_id, text='Привет, давай пообщаемся?')
AttributeError: 'Update' object has no attribute 'send_message'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-07-08
@SoreMix

Here is an example
https://github.com/python-telegram-bot/python-tele...
Accordingly, most likely it will be correct

update.message.reply_text('Привет, давай пообщаемся?')

https://python-telegram-bot.readthedocs.io/en/stab...
https://python-telegram-bot.readthedocs.io/en/stab...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question