如何将值存入webhook PHP for telegram bot

时间:2016-06-20 12:50:36

标签: php mysql 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 with tutorial ...
  • 点击/DOMANDE搜索,写一个单词,进入mysql db with question ...

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

2 个答案:

答案 0 :(得分:3)

您可以使用InlineKeyboardMarkup代替ReplyKeyboardMarkup。内联键盘返回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"
  }
}

答案 1 :(得分:0)

我更喜欢使用php会话。 所以我使用Curl调用一个php页面来存储和读取mysql db:

中的会话
$link = mysqli_connect(...) or die("Error " . mysqli_error($link));
$query = "SELECT * FROM MMT_BOT WHERE CHAT_ID = '".$_POST["chat"]."'" or die("Error in the consult.." . mysqli_error($link));
$result = mysqli_query($link, $query);
$res_num = mysqli_num_rows($result);
if ($res_num === 0) {
        $query = "INSERT INTO MMT_BOT (CHAT_ID, SESSION) VALUES ('".$_POST["chat"]."','".$_POST["session"]."')" or die("Error in the consult.." . mysqli_error($link));
        $link->query($query);
        $query = "SELECT * FROM MMT_BOT WHERE CHAT_ID = '".$_POST["chat"]."'" or die("Error in the consult.." . mysqli_error($link));
        $result = mysqli_query($link, $query);
    } else {
        $query = "UPDATE MMT_BOT SET SESSION = '".$_POST["session"]."' WHERE CHAT_ID = '".$_POST["chat"]."'" or die("Error in the consult.." . mysqli_error($link));
        $link->query($query);
        $query = "SELECT * FROM MMT_BOT WHERE CHAT_ID = '".$_POST["chat"]."'" or die("Error in the consult.." . mysqli_error($link));
        $result = mysqli_query($link, $query);
    }
        $rows = array();
            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
              $rows[] = array('sessione' => $row['SESSION'],'chat_id' => $row['CHAT_ID']);
        }
echo json_encode($rows);

所以我可以存储每个答案的步骤并根据步骤提出问题! 也可用于存储用户信息!