How to store value into session on webhook PHP for telegram bot
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
/GUIDEto search, writing a word, into mysql db with tutorial ... - clicking
/DOMANDEto 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?
php mysql telegram-bot
add a comment |
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
/GUIDEto search, writing a word, into mysql db with tutorial ... - clicking
/DOMANDEto 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?
php mysql telegram-bot
1
Try save key-value pair to file or database. key may be$chat_id
– Dmitry
Jun 21 '16 at 12:48
add a comment |
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
/GUIDEto search, writing a word, into mysql db with tutorial ... - clicking
/DOMANDEto 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?
php mysql telegram-bot
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
/GUIDEto search, writing a word, into mysql db with tutorial ... - clicking
/DOMANDEto 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?
php mysql telegram-bot
php mysql telegram-bot
edited Jun 20 '16 at 13:06
Timothy
2,05431825
2,05431825
asked Jun 20 '16 at 12:50
DigitalXPDigitalXP
87415
87415
1
Try save key-value pair to file or database. key may be$chat_id
– Dmitry
Jun 21 '16 at 12:48
add a comment |
1
Try save key-value pair to file or database. key may be$chat_id
– Dmitry
Jun 21 '16 at 12:48
1
1
Try save key-value pair to file or database. key may be
$chat_id– Dmitry
Jun 21 '16 at 12:48
Try save key-value pair to file or database. key may be
$chat_id– Dmitry
Jun 21 '16 at 12:48
add a comment |
2 Answers
2
active
oldest
votes
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"
}
}
add a comment |
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!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37923055%2fhow-to-store-value-into-session-on-webhook-php-for-telegram-bot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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"
}
}
add a comment |
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"
}
}
add a comment |
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"
}
}
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"
}
}
edited Nov 15 '18 at 21:25
answered Jun 23 '16 at 18:48
Behzad SeyfiBehzad Seyfi
972822
972822
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered Jun 27 '16 at 12:37
DigitalXPDigitalXP
87415
87415
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37923055%2fhow-to-store-value-into-session-on-webhook-php-for-telegram-bot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Try save key-value pair to file or database. key may be
$chat_id– Dmitry
Jun 21 '16 at 12:48