Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create own bot on telegram with php

I saw a couple of days ago this tutorial on youtube. It was very interesting, so I decided to make a own bot. I used the code from the tutorial as a template:

<?php

$bottoken = "*****";
$website = "https://api.telegram.org/bot".$bottoken;


$update = file_get_contents('php://input');

$updatearray = json_decode($update, TRUE);

$length = count($updatearray["result"]);
$chatid = $updatearray["result"][$length-1]["message"]["chat"]["id"];
$text = $updatearray["result"][$length-1]["message"]["text"];

if($text == 'hy'){
    file_get_contents($website."/sendmessage?chat_id=".$chatid."&text=hello");
} 
elseif($text == 'ciao'){
    file_get_contents($website."/sendmessage?chat_id=".$chatid."&text=bye");
}

The script worked if I execute the script manually. However when I use the webhook it doesn't work anymore. The tutorial said that $update = file_get_contents('php://input'); is the right way, to be used before $update = file_get_contents($website."/getupdates");. My question how can I use php://input to execute my script automatically? The script is on a server from "one.com" and the certificate is also from "one.com".

like image 889
Mark Decker Avatar asked Aug 23 '15 12:08

Mark Decker


People also ask

Can I create Telegram bot with PHP?

We created telegram bot on PHP, which can save user name and surname from chat. It is really simple telegram bot!

Is Telegram bot paid?

Telegram Bot Payments are a free and open platform that allows sellers to accept payments for goods and services from Telegram users. Telegram doesn't collect payment information and takes no commission.


2 Answers

If you use selfsigned ssl you have to point to the ssl path ,, use the ssh to run this command after filling it with your real data ,,

curl -F "url=https://example.com/myscript.php" -F "certificate=@/etc/apache2/ssl/apache.crt" https://api.telegram.org/bot<SECRETTOKEN>/setWebhook
like image 95
user3583958 Avatar answered Oct 04 '22 10:10

user3583958


After change to WebHook method, you need to put as follows, since now we are going to handle one message at time. For me works perfectly.

instead

$chatId = $updateArray["result"][0]["message"]["chat"]["id"];

to

$chatID = $update["message"]["chat"]["id"];
like image 28
Michel Fernandes Avatar answered Oct 04 '22 09:10

Michel Fernandes