Reputation: 31
I brought an easy example to better understand my problem.
I have these codes that you enter name and text to then get the values with Get.
Once received the values, the program will send in a telegram bot created by me with a sendMessage
.
$chat_id =
to the chat id of the telegram bot I created.
<?php
if(isset($_GET['name']) && isset($_GET['message'])){
define ('url',"https://api.telegram.org/botTOKEN/");
$name = $_GET['name'];
$message = $_GET['message'];
$chat_id = 'TEST';
function invia($chat_id,$message = ''){
$url= url."sendMessage?chat_id=$chat_id&parse_mode=HTML&text=".$message;
file_get_contents($url);
}
$message = urlencode("Hello $name this is the message : $message");
invia($chat_id, $message);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name:<br>
<input type="text" name="name" placeholder="Your Name">
<br>
Message:<br>
<textarea name="message" placeholder="Your Message Here" rows="6" cols="33"></textarea>
<br><br>
<input type="submit" value="Submit">
</form>
Let’s write for example Name: "Lorenzo Poggi" and Message: "Hi, this is a test".
Once sent, I will receive the message directly in the chat of the telegram bot.
Now the question is this, but if in the example.php
page, where I have all the updates of the telegram bot in question thanks to setWebhook
, I would like to get the writing "is a test" from the text of the message sent previously with sendMessage
, How can I do that?
Upvotes: 1
Views: 2405
Reputation: 2106
The result of Telegram response should be something like this :
{
"update_id": 901816057,
"message": {
"message_id": 36089,
"from": {
"id": 223110107,
"first_name": "Ğąme",
"last_name": "Ǿver!",
"username": "GameO7er"
},
"chat": {
"id": 223110107,
"first_name": "Ğąme",
"last_name": "Ǿver!",
"username": "GameO7er",
"type": "private"
},
"date": 1490493909,
"text": "Hi, this is a test"
}
}
You can log the response and try to decode it via this website Then if you wanna to get/print message of users you should use this way :
function invia(){
$url= url."sendMessage?chat_id=$chat_id&parse_mode=HTML&text=".$message;
$TelegramResult = file_get_contents($url);
$UserText = $TelegramResult['message']['text']; // Hi, this is a test
}
as you can see $UserText
contains the text that your user sent to the bot.
Let me if you have any question. also my Telegram id is shown in the above source so feel free to contact me.
Upvotes: 2