代码之家  ›  专栏  ›  技术社区  ›  Gen Tan

在telegram bot上发送本地托管的照片

  •  1
  • Gen Tan  · 技术社区  · 6 年前

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

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

    这很管用。我的电报用户能收到“消息”。

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

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

    我没有任何例外,但是,我的用户没有收到图像。我在Jupyter笔记本上得到的输出是: <Response [414]>

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

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

    我在Mac操作系统上运行这个。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Artem Rys    6 年前

    请试试这个:

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

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

    希望,对你有用。