【问题标题】:Sending photo from URL with Telegram Bot使用 Telegram Bot 从 URL 发送照片
【发布时间】:2015-09-20 22:12:47
【问题描述】:

我制作了一个电报机器人,它使用 pyTelegramBotAPI 包装器根据 URL 的请求发送照片。所以我尝试放置一个虚拟照片 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)

我不确定我是否错过了什么。

【问题讨论】:

    标签: python telegram-bot


    【解决方案1】:

    试试这个:

    import telebot
    import time
    import urllib
    
    url = 'http://scontent-b.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/10919672_584633251672188_179950734_n.jpg'
    f = open('out.jpg','wb')
    f.write(urllib.request.urlopen(url).read())
    f.close()
    
    def listener(*messages):
        for m in messages:
            chat_id = m.chat.id
            if m.content_type == 'text':
                text = m.text
                msgid = m.message_id
                if text.startswith('/photo'):
                    tb.send_chat_action(chat_id, 'upload_photo')
                    img = open('out.jpg', 'rb')
                    tb.send_photo(chat_id, img, reply_to_message_id=msgid)
                    img.close()
    
    
    tb = telebot.TeleBot(TOKEN)
    tb.set_update_listener(listener) #register listener
    tb.polling()
    
    while True:
        time.sleep(0)
    

    或(使用 pyTelegramBotAPI 0.2.0)

    import telebot
    import time
    import urllib
    
    url='http://scontent-b.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/10919672_584633251672188_179950734_n.jpg'
    f = open('out.jpg','wb')
    f.write(urllib.request.urlopen(url).read())
    f.close()
    
    tb = telebot.TeleBot(TOKEN)
    
    @tb.message_handler(commands=['photo'])
    def send_photo(message):
        tb.send_chat_action(message.chat.id, 'upload_photo')
        img = open('out.jpg', 'rb')
        tb.send_photo(message.chat.id, img, reply_to_message_id=message.message_id)
        img.close()
    
    tb.polling()
    
    while True:
        time.sleep(0)
    

    【讨论】:

    • 所以我们需要先将整个文件下载到本地文件夹,然后再上传到 Telegram。鉴于我完全在我的机器上运行脚本,有什么方法可以在下载时减小图像大小?
    • 我不知道你到底想要做什么,但我确信有办法用 python 压缩图像,但不是 while 下载。当您不再需要该文件时,请务必从您的计算机中删除该图像。
    • 我的代码在这里——我相信我是 python 新手——不需要将图像下载到本地文件夹:stackoverflow.com/a/32441772/1097372
    【解决方案2】:
    elif 'Hi' in text:
        reply(img=urllib2.urlopen('img url').read())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 2021-10-03
      • 2021-04-24
      • 2018-11-12
      • 2015-11-24
      • 2016-07-09
      • 2021-08-03
      相关资源
      最近更新 更多