无法通过 Telegram Bot 发送图像

问题描述

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

解决方法

您的图片网址不正确,它会转到包含其他元素以及图片本身的页面。 您图片的正确网址是: 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)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...