【问题标题】:Sending locally hosted photo on telegram bot在电报机器人上发送本地托管的照片
【发布时间】:2019-05-14 21:41:40
【问题描述】:

我正在使用 api.telegram.bot 和 requests 发送消息和图像。

requests.get(url + 'sendMessage', params=dict(chat_id=send_to_user_id,text="Messo"))

这工作正常。我的电报用户能够收到消息“Messo”。

现在,我正在尝试使用 sendPhoto 发送我在本地驱动器上托管的图像。

path = "kings/test_screenie1.png"
requests.get(url + 'sendPhoto', params=dict(chat_id=send_to_user_id, photo=open(path,'rb')))

我没有收到任何异常,但是,我的用户没有收到图像。我在 Jupyter notebook 中得到的输出是:<Response [414]>

运行此代码的我的 .ipynb 文件位于:/Users/abc/Desktop/webproject/play0.ipynb

我的图片文件位于:/Users/abc/Desktop/webproject/kings/test_screenie1.png

我在 Mac OS 上运行它。

【问题讨论】:

    标签: python telegram telegram-bot python-telegram-bot


    【解决方案1】:

    请试试这个:

    requests.post(url + 'sendPhoto', data={'chat_id': send_to_user_id}, files={'photo': open('/Users/abc/Desktop/webproject/kings/test_screenie1.png', 'rb')})
    

    我已经在我的机器人上进行了本地测试,这种方法适合我。

    希望,对你有用。

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 2016-10-17
      • 2019-03-28
      • 2020-07-09
      • 1970-01-01
      • 2021-04-20
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多