2016-09-25 688 views
1

我有我用我的電報機器人代碼(Python)的這一部分:如何在Telegram bot(Python)中使用inline_keyboard而不是鍵盤?

def reply(msg=None, img=None): 
     if msg: 
      resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({ 
       'chat_id': str(chat_id), 
       'text': msg.encode('utf-8'), 
       'disable_web_page_preview': 'true', 
       # 'reply_to_message_id': str(message_id), 
       'reply_markup': json.dumps({'keyboard': [bline1, bline2], 'resize_keyboard': True}), 
      })).read() 

對於這部分一切工作正常。問題是:如何使用常規鍵盤的inline_keyboard intead?

我知道這是一個noob問題,但如果有人可以幫助我,那將會很棒。

謝謝!

回答

1

因爲Inline Keyboard只是一個不同的json對象,我只能說你只需要用json.dumps而不是你目前的版本來構建它。繼你的榜樣,這樣的事情應該做的伎倆:

def reply(msg=None, img=None): 
     if msg: 
      resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({ 
       'chat_id': str(chat_id), 
       'text': msg.encode('utf-8'), 
       'disable_web_page_preview': 'true', 
       # 'reply_to_message_id': str(message_id), 
       'reply_markup': json.dumps({'inline_keyboard': [[{'text': bline1, 'callback_data': bline1}, {'text': bline2, 'callback_data': bline2}]]}), 
      })).read() 
+0

謝謝你,但遺憾的是它不工作 - 機器人只是停止響應,我已經試過這一點。斯蒂爾找不出什麼問題。 –

+0

'json_keyboard = json.dumps({keym:[[{'text':'b2','callback_data':'6'}]]})' - 這種方法奏效,謝謝! –