2016-02-09 77 views
0

完整的Python新手,對不起!電報Python聊天機器人 - 用動畫gif回覆

我使用Telebot入門套件(https://github.com/yukuku/telebot),其中處理答覆如下:

elif 'who are you' in text: 
     reply('telebot starter kit, created by yukuku: https://github.com/yukuku/telebot') 

我能得到它與圖像回覆:

elif 'Test1' in text: 
      reply(img=urllib2.urlopen('https://i.ytimg.com/vi/VC8H5B2YVCY/maxresdefault.jpg').read()) 

,但不能發送動畫GIF。作爲一個img發送每上述使用sendPhoto是靜態的

我相信它必須是添加InlineQueryResultGif類並在回覆()中調用它的情況,但我已經嘗試了很多很多方法來做但我沒有取得任何進展

幫助!

EDIT顯示一些嘗試:

首先我試圖編輯一個已經到位發送的IMG的elif的說法:

elif gif: 
      gif = multipart.post_multipart(BASE_URL + 'InlineQueryResultGif', [ 
       ('chat_id', str(chat_id)), 
       ('reply_to_message_id', str(message_id)), 
      ], [ 
       ('gif', 'image.gif', gif), 
      ]) 

,然後簡單地改變的答覆是:

elif 'Test4' in text: 
     reply(gif=urllib2.urlopen('http://www.reactiongifs.us/wp-content/uploads/2014/08/popcorn_indiana_jones.gif').read()) 

然後我嘗試添加InlineQueryResultGif類本身:

class InlineQueryResult: 
pass 

class InlineQueryResultGif(InlineQueryResult): 
""" Represents a link to an animated GIF file. By default, this animated GIF file will be sent by the user with 
optional caption. Alternatively, you can provide message_text to send it instead of the animation. 
Attributes: 
    id       (str) :Unique identifier of this result 
    gif_url      (str) :A valid URL for the GIF file. File size must not exceed 1MB 
    gif_width     (int) :*Optional.* Width of the GIF 
    gif_height     (int) :*Optional.* Height of the GIF 
    thumb_url     (str) :*Optional.* URL of a static thumbnail for the result (jpeg or gif) 
    title      (str) :*Optional.* Title for the result 
    caption      (str) :*Optional.* Caption of the GIF file to be sent 
    message_text    (str) :*Optional.* Text of a message to be sent instead of the animation 
    parse_mode     (str) :*Optional.* Send 「Markdown」, if you want Telegram apps to show bold, 
                 italic and inline URLs in your bot's message. 
    disable_web_page_preview (bool) :*Optional.* Disables link previews for links in the sent message 
""" 

def __init__(self, id, gif_url, 
      gif_width=None, gif_height=None, thumb_url=None, title=None, 
      caption=None, message_text=None, parse_mode=None, disable_web_page_preview=None): 
    self.type = 'gif' 
    self.id = id 
    self.gif_url = gif_url 
    self.gif_width = gif_width 
    self.gif_height = gif_height 
    self.thumb_url = thumb_url 
    self.title = title 
    self.caption = caption 
    self.message_text = message_text 
    self.parse_mode = parse_mode 
    self.disable_web_page_preview = disable_web_page_preview 

並試圖把它的回覆裏面:

elif 'Test2' in text: 
     reply(InlineQueryResultGif('http://www.reactiongifs.us/wp-content/uploads/2014/08/popcorn_indiana_jones.gif')) 

和很多不同的版本以上的。沒有得到任何工作

+0

「我已經嘗試了很多很多的這樣做的方式,但我沒有取得任何進展」你爲什麼不告訴我們一些那些方式?你可能想要閱讀[MCVE],這對寫一個好問題很有幫助。 –

+0

謝謝,編輯帖子添加一些我已經嘗試過的東西 –

回答

0

我使用python-telegram-bot,這是我如何接近這一點。

def sendImage(bot, update, dataval): # Funtion to send images or gifs the proper way 
    val = dataval.rsplit('.', 1)[1] 
    if val == 'gif': 
    # Send a gif 
    bot.sendDocument(chat_id=update.message.chat_id, document = dataval) 
    elif val == 'webm': 
    bot.sendMessage(chat_id=update.message.chat_id, text = "The item attempted to be sent is unsupported at the moment.") 
    else: 
    # Send a Picture 
    bot.sendPhoto(chat_id=update.message.chat_id, photo=dataval) 

在我上面的示例中,我將一個url傳遞給dataval變量中的函數。我沒有在幾個月內重新訪問過,所以,它現在可能支持webm。此外,還有一種方法可以通過電報的服務器上存儲的ID來發送圖像,但是,我不知道如何使用該方法。

0

你幾乎擁有了在第一時間實現在線的東西之前,除非你需要使用sendDocument

elif gif: 
     resp = multipart.post_multipart(BASE_URL + 'sendDocument', [ 
      ('chat_id', str(chat_id)), 
      ('reply_to_message_id', str(message_id)), 
     ], [ 
      ('gif', 'image.gif', gif), 
     ]) 

...然後用urllib2讀取GIF,像這樣:

elif 'Test2' in text: 
    reply(gif=urllib2.urlopen('http://www.reactiongifs.us/wp-content/uploads/2014/08/popcorn_indiana_jones.gif').read()) 

您還可以使用本地文件做到這一點,但我發現file://功能在urllib2相對路徑(如與谷歌的AppEngine打交道時是相當令人沮喪)。對於本地文件,我只是用urllib代替,就像這樣:

elif 'Test2' in text: 
    reply(gif=urllib.urlopen('images/animated.gif').read())