我做了一个电报机器人,它根据请求使用pyTelegramBotAPI包装器发送照片.所以我尝试放置一个虚拟照片URL并测试机器人是否可以发送图像,但它失败并出现以下错误.
telebot.apihelper.ApiException: sendPhoto failed. Returned result: <Response [400]>
我不确定错误是什么,但如何正确地使用Telegram Bot API从URL发送照片?这是我的代码
import telebot
import time
import urllib
from io import BytesIO
from PIL import Image
TOKEN = '<token here>'
url='http://scontent-b.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/10919672_584633251672188_179950734_n.jpg'
def listener(*messages):
for m in messages:
chatid = m.chat.id
if m.content_type == 'text':
text = m.text
name = m.fromUser.first_name
msgid = m.message_id
if(text.startswith('/photo')):
img = BytesIO(urllib.request.urlopen(url).read())
tb.send_chat_action(chatid, 'upload_photo')
tb.send_photo(chatid, img, reply_to_message_id=msgid)
tb = telebot.TeleBot(TOKEN)
tb.get_update() # cache exist message
tb.set_update_listener(listener) #register listener
tb.polling()
while True:
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
我不确定我是否错过了一些东西.
我想根据要求发送图像(通过 URL 或路径)。我在这里使用源代码。 该代码已经有一个发送图像的示例(通过 URL 或路径),但我不明白,因为我是 Python 的新手。
这是示例代码片段:
elif text == '/image': #request
img = Image.new('RGB', (512, 512))
base = random.randint(0, 16777216)
pixels = [base+i*j for i in range(512) for j in range(512)] # generate sample image
img.putdata(pixels)
output = StringIO.StringIO()
img.save(output, 'JPEG')
reply(img=output.getvalue())
Run Code Online (Sandbox Code Playgroud)
一些 API 信息可以在这里找到。
谢谢你的耐心。
我正在尝试导入我的应用程序的请求模块,我想在Google App Engine上本地查看.我收到日志控制台错误告诉我"没有这样的模块存在".
我已经在命令行中安装了它(使用pip),甚至试图将它安装在我的项目目录中.当我这样做时,shell告诉我:
"要求已经满足(使用--upgrade升级):/ Library /Python/2.7/site-packages中的请求".
App Engine告诉我模块不存在,shell说它已经安装了.
我不知道这是否是路径问题.如果是这样,我可以在我的mac中找到的唯一App Engine相关应用程序是启动器?