【问题标题】:telegram bot - Chat not found电报机器人 - 未找到聊天
【发布时间】:2021-02-14 20:03:11
【问题描述】:

我对电报机器人完全陌生。我想在以下代码中执行bot.send_message()(如代码中所述),但不能这样做。请帮忙。

!pip install python-telegram-bot --upgrade
import telegram
bot = telegram.Bot(token='**************')
bot.send_message(chat_id='******', text="I'm sorry Dave I'm afraid I can't do that.")

错误如下:

1 bot.send_message('mardepbot',"I'm sorry Dave I'm afraid I can't do that.")

/usr/local/lib/python3.6/dist-packages/telegram/bot.py in decorator(self, *args, **kwargs)
     63     def decorator(self, *args, **kwargs):
     64         logger.debug('Entering: %s', func.__name__)
---> 65         result = func(self, *args, **kwargs)
     66         logger.debug(result)
     67         logger.debug('Exiting: %s', func.__name__)

/usr/local/lib/python3.6/dist-packages/telegram/bot.py in decorator(self, *args, **kwargs)
     88                 data['reply_markup'] = reply_markup
     89 
---> 90         result = self._request.post(url, data, timeout=kwargs.get('timeout'))
     91 
     92         if result is True:

/usr/local/lib/python3.6/dist-packages/telegram/utils/request.py in post(self, url, data, timeout)
    307             result = self._request_wrapper('POST', url,
    308                                            body=json.dumps(data).encode('utf-8'),
--> 309                                            headers={'Content-Type': 'application/json'})
    310 
    311         return self._parse(result)

/usr/local/lib/python3.6/dist-packages/telegram/utils/request.py in _request_wrapper(self, *args, **kwargs)
    221             raise Unauthorized(message)
    222         elif resp.status == 400:
--> 223             raise BadRequest(message)
    224         elif resp.status == 404:
    225             raise InvalidToken()

BadRequest: Chat not found

【问题讨论】:

  • 您要向其发送消息的用户应该在您的机器人中启动或至少执行一项操作,然后您就可以向他发送任何消息。

标签: telepot


【解决方案1】:

我刚刚解决了类似的问题。请检查:

  1. 您的机器人设置组隐私必须关闭(机器人父亲 -> 组隐私 -> 禁用)。
  2. 如果您的聊天机器人已添加到聊天中,请将其删除,然后重新添加
  3. 确保您的聊天 ID 前有一个“-”号,因此如果您的聊天链接如下所示:https://web.telegram.org/#/im?p=g123456789,那么您的 chat_id = '-123456789'

【讨论】:

【解决方案2】:

您不能将消息发送到字符串 ('mardepbot') 作为地址。如果 mardepbot 是您正在使用的机器人,那么您犯了 2 个错误;那么您应该使用自己的 ID 向自己发送消息。

【讨论】:

    猜你喜欢
    • 2017-05-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多