2017-06-14 1 views
1

私は私の電報ボットメッセージに次のようにカスタムボタンを追加したいと思いますが、私はPythonのPythonのボタンのオプション

と私のメッセージにそれらを埋め込む方法見当がつかない私は埋め込むしたいと思いますそれをこのメッセージに入れます。

def run(results, index, msgText): 
# The message below this! 
    msg = '<b>Hello there, ' + results[index]['message']['from']['username'] + '!</b> I am <b>Makuna Hattata</b>, a spin-off bot of the original <b>Hakuna Mattata</b> bot \n\nI am a semi-smart bot capable of understanding basic commands, which you can see by pressing the button below!' 
    try: 
     sendMessage(index, results, msg, '&parse_mode=html') 
     logWorked(results[index]['message']['from']['username'], results[index]['message']['from']['id'], "/start message sent") 
    except Exception as err: 
     logError(results[index ]['message']['from']['username'], results[index]['message']['from']['id'], "/start message sent", err) 

答えて

0

カスタムキーボードを送ることができます。まず、telegramapiが必要です。 ReplyKeyboardMarkupcommandはいつでも使用できます。追加何もインポートしたくない場合は

keyboard = [["key1", "key2"], ["return"]] 
reply_markup = ReplyKeyboardMarkup.create(keyboard) 
bot.send_message(user_id, 'testing keyboard', reply_markup=reply_markup) 

あなたは次の関数を使用することができます。それは次のようになります(実際にあなたがしなければと連携するためのツールの多くを与えるだろう!):

def send_keyboard(token, chat_id, text, keyboard=[], one_time=True, resize=True): 

    if text is None or len(text) <= 0: 
     return None 

    post_data = { 
     "chat_id": chat_id, 
     "text": text 
    } 

    if len(keyboard) == 0: 
     return None 

    post_data["reply_markup"] = { 
     "keyboard": keyboard, 
     "one_time_keyboard": one_time, 
     "resize_keyboard": resize 
    } 

    headers = { 
     "Content-Type": "application/json", 
     "Content-Length": len(json.dumps(post_data)) 
    } 

    url = TELEGRAM_HOSTNAME + "/bot" + token + "/sendMessage" 
    try: 
     response = requests.post(url, headers=headers, data=json.dumps(post_data)) 
    except: 
     return None 

    if response.status_code >= 400: 
     return None 

    return response 

ちょうどchat_id、トークン、メッセージテキスト、キーボードのキーを渡す必要があります。

+0

apiなしでそれを行う方法はありますか?私はAPIを使用しようとしていません。 – Splinxyy

+0

いくつかのコードを追加してチェックアウトし、問題が解決するかどうかを確認します。 – cookiedough

+0

それで、短縮しました、私は 'reply_markup'パラメータでsendmessageに送りますか? – Splinxyy

関連する問題