2016-09-25 2 views
1

私は私の電報ボットで使用しているコードのこの部分(パイソン)を持っている: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を使用する方法ですか?

私はそれがノブの質問であることを理解していますが、誰かが私を助けることができれば素晴らしいと思います。

ありがとうございます!

答えて

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'}]]})' - このアプローチはうまくいきました。 –

関連する問題