【问题标题】:Not able to send Images via Telegram Bot无法通过 Telegram Bot 发送图像
【发布时间】:2021-09-14 23:02:06
【问题描述】:

当我发出命令时,它只是没有响应。我所有的其他命令都工作正常。我正在使用pytelegrambotapi

我的代码-

import telebot
from PIL import Image
import requests
from io import BytesIO
    
#This is my image link
IMAGE_LINK = "https://pixabay.com/images/id-1127657/"
    
@bot.message_handler(commands=['image'])
def image(message):
    response = requests.get(IMAGE_LINK)
    img = Image.open(BytesIO(response.content))
    #send the photo
    bot.send_photo(message.chat.id, img)

【问题讨论】:

  • 考虑到您传递的链接,我认为您发送的是 html,而不是照片。您还可以捕获 send_photo 可能触发的任何异常,然后查看实际问题。

标签: python bots telegram telegram-bot py-telegram-bot-api


【解决方案1】:

您的图片网址不正确,它与图片本身一起转到包含其他元素的页面。 您的图片的正确网址是: https://cdn.pixabay.com/photo/2016/01/08/11/49/text-1127657_960_720.jpg

您也可以将此链接直接传递给send_photo,这样Telegram 本身就会从该网址下载并发送照片:

IMAGE_LINK = "https://cdn.pixabay.com/photo/2016/01/08/11/49/text-1127657_960_720.jpg"


@bot.message_handler(commands=['image'])
def image(message):
    bot.send_photo(message.chat.id, IMAGE_LINK)

【讨论】:

  • 你能告诉我应该在需求文件中输入什么以便将此代码部署到 Heroku 吗?
  • 嘿。为其他人创建另一个问题也可以发起。
猜你喜欢
  • 2019-11-18
  • 2018-06-07
  • 1970-01-01
  • 2017-06-06
  • 2016-05-09
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多