2015-07-03 13 views
6

के साथ यूआरएल से फोटो भेजना मैंने एक टेलीग्राम बॉट बनाया जो pyTelegramBotAPI wrapper का उपयोग कर यूआरएल से अनुरोध पर फोटो भेजता है। तो मैंने एक डमी फोटो यूआरएल डालने की कोशिश की और परीक्षण किया कि क्या बॉट छवि भेज सकता है लेकिन यह निम्न त्रुटि से विफल रहा है।टेलीग्राम बॉट

telebot.apihelper.ApiException: sendPhoto failed. Returned result: <Response [400]>

मुझे यकीन है कि क्या त्रुटि है नहीं कर रहा हूँ, लेकिन मैं कैसे टेलीग्राम बॉट एपीआई सही ढंग से उपयोग 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) 

मुझे यकीन नहीं है कि मुझे कुछ याद आया है या नहीं।

उत्तर

6

इस प्रयास करें:

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) 
+0

तो हम सब के बाद टेलीग्राम को अपलोड करने से पहले स्थानीय फ़ोल्डर में पूरी फ़ाइल डाउनलोड करना होगा। डाउनलोड करते समय छवि आकार को कम करने का कोई तरीका है, क्या मैंने स्क्रिप्ट को पूरी तरह से अपनी मशीन पर चलाया है? – Zerocchi

+0

मुझे नहीं पता कि यह वास्तव में क्या है जो आप बनाने की कोशिश कर रहे हैं, लेकिन मुझे यकीन है कि पाइथन के साथ छवियों को संपीड़ित करने के तरीके हैं, केवल _while_ डाउनलोड नहीं। और जब आपको फ़ाइल की आवश्यकता नहीं होती है तो अपने कंप्यूटर से छवि को हटाना सुनिश्चित करें। – Pete

+0

मेरा कोड यहां - मुझे विश्वास है क्योंकि मैं पाइथन के लिए नया हूं - छवि को स्थानीय फ़ोल्डर में डाउनलोड करने की आवश्यकता नहीं है: http://stackoverflow.com/a/32441772/1097372 – Iyas

2
elif 'Hi' in text: 
    reply(img=urllib2.urlopen('img url').read()) 
संबंधित मुद्दे