无法通过 Telegram Bot 发送图像

时间:2021-07-03 08:28:46

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

当我发出命令时,它只是没有响应。我所有的其他命令都工作正常。我正在使用 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)

1 个答案:

答案 0 :(得分:1)

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

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

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)