编程语言
首页 > 编程语言> > 如何将值存储到Webhook PHP for Telegram bot的会话中

如何将值存储到Webhook PHP for Telegram bot的会话中

作者:互联网

为了解决这个问题,我让自己发疯.
我有一个PHP webhook页面,如下所示:

function processMessage($message)
{
    if (isset($message['text'])) {
        $text = $message['text'];

        if (strpos($text, "/start") === 0) {
            apiRequestJson("sendMessage", array(
                'chat_id'      => $chat_id,
                "text"         => 'Benvenuto ' . $firstname . ' ' . $lastname . ' sul BOT di MIMANCHITU, dimmi cosa vuoi fare?',
                'reply_markup' => array(
                    'keyboard'          => array(array('/GUIDE', '/DOMANDE')),
                    'one_time_keyboard' => true,
                    'resize_keyboard'   => true
                )
            ));

        } else if ($text === "/DOMANDE") {
            apiRequest("sendMessage", array(
                'chat_id' => $chat_id,
                "text"    => 'Inserisci la parola da cercare tra le risposte della Dottoressa [' . $azione . '] XXX:'
            ));

        } else if (strpos($text, '/') !== true) {
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_VERBOSE        => true,
                CURLOPT_URL            => 'http://www.domain.it/bot/search_dom.php',
                CURLOPT_POSTFIELDS     => array(
                    'parola' => $text
                )
            ));

            $resp = curl_exec($curl);
            $obj  = json_decode($resp);
            curl_close($curl);

            foreach ($obj as $value) {
                apiRequest(......));
            }

        } else if ($text === "/GUIDE") {
            apiRequest("sendMessage", array(
                'chat_id' => $chat_id,
                "text"    => 'Cerca una parola per visualizzare i contenuti trovati tra le Guide al Sesso di MIMANCHITU:'
            ));
        }
    }
}

用户有两个选择:

>单击/ GUIDE进行搜索,用教程将单词写到mysql db中…
>单击/ DOMANDE进行搜索,将一个单词写到有问题的mysql数据库中…

我的问题是从自定义键盘选择按钮后,如何检查用户是否在/ GUIDE或/ DOMANDE中搜索!我当时正在考虑设置PHP SESSION参数,但这不起作用!
有任何想法吗?

解决方法:

您可以使用InlineKeyboardMarkup代替ReplyKeyboardMarkup.内联keayboards返回query_callback,其中包括以前的消息,以及用户对该消息的答复.这是按下inline_keyboard时来自Telegram的示例JSON回复:

{
  "update_id": 88888888,
  "callback_query": {
    "id": "99999999999999999",
    "from": {
      "id": XXXXXXXXXX,
      "first_name": "ABCD",
      "last_name": "CDEF"
    },
    "message": {
      "message_id": 56,
      "from": {
        "id": YYYYYYYYYY,
        "first_name": "myBotName",
        "username": "myBot"
      },
      "chat": {
        "id": XXXXXXXXXX ,
        "first_name": "ABCD",
        "last_name": "CDEF",
        "type": "private"
      },
      "date": 1466703216,
      "text": "someText"
    },
    "data": "returnValue"
  }
}

标签:telegram-bot,mysql,php
来源: https://codeday.me/bug/20191118/2028706.html