2

I am making myself crazy trying to solve this problem.
I have a PHP webhook page like this:

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:'
            ));
        }
    }
}

The user has two choices:

  • clicking /GUIDE to search, writing a word, into mysql db with tutorial ...
  • clicking /DOMANDE to search, writing a word, into mysql db with question...

My problem is how to check if user searches in /GUIDE or /DOMANDE after choosing the button from a custom keyboard! I was thinking about setting a PHP SESSION parameter, but that doesn't work! Any ideas?

Timothy
  • 2,004
  • 3
  • 23
  • 29
DigitalXP
  • 179
  • 5
  • 18

2 Answers2

2

You can use InlineKeyboardMarkup instead of ReplyKeyboardMarkup. inline keayboards return query_callback that include former massage and also user reply to that massage. this is sample JSON reply from Telegram when an inline_keyboard is pressed:

{
  "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"
  }
}
Seyfi
  • 1,832
  • 1
  • 20
  • 35
-1

I preferr use php session. So I use Curl to call a php page to store and read session into 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);

So I could store the step of each answer and make question depending the step! Is useful also to store user info!

DigitalXP
  • 179
  • 5
  • 18