Telegram Bot setWebhook problem

lixiang9779

Member
Joined
Jul 19, 2009
Messages
483
Reaction score
0
Anyone have experience in making a telegram bot?

I am not able to use setwebhook despite having a ssl certified domain name.

when i message my bot it doesnt respond, but i am able to send out message if it is using getUpdates method.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Anyone have experience in making a telegram bot?

I am not able to use setwebhook despite having a ssl certified domain name.

when i message my bot it doesnt respond, but i am able to send out message if it is using getUpdates method.

Invoke the getWebhookInfo API method and find out what is wrong with your web hook. Below is what I get when I did not have a public CA certificate

Code:
curl 'https://api.telegram.org/bot<MY_API_TOKEN>/getWebhookInfo'

{"ok":true,"result":{"url":"https://my.callback.host/","has_custom_certificate":false,"pending_update_count":32,"last_error_date":1486903822,"last_error_message":"SSL error {336134278, error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed}","max_connections":40}}


I have to thank that you have this enquiry, so I get to play with the Telegram Bot API.

I have used Cloudflare to provide a public CA SSL certificate for Telegram client.

This is what I did to configure Telegram bot webhook
Code:
curl -F 'url=https://host.domain/johnlittle/index.php' -F 'max_connections=1' -F 'allowed_updates=' 'https://api.telegram.org/bot34******31:***********/setWebhook'

This is what my PHP code looks like
Code:
# cat index.php
<?php
$body = file_get_contents('php://input');
error_log($body);
?>

When the bot receive any messages, for example, a "testing 123" message
35avjlw.png


This is shown in the server error logs
Code:
2017/02/12 22:01:13 [error] 6699#6699: *4337 FastCGI sent in stderr: "PHP message: {"update_id":926****412,
"message":{"message_id":70,"from":{"id":21****509,"first_name":"*****","last_name":"*****","username":"******"},"chat":{"id":21*****09,"first_name":"*****","last_name":"*****","username":"*****","type":"private"},"date":1486908071,"text":"testing 123"}}" while reading response header from upstream, client: 127.0.0.1, server: _, request: "POST /johnlittle/index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "*******"

I have masked out confidential information with the structure retained.


Update: If you would like to use a self-signed certificate, this would be how you setup the webhook
Code:
curl -F 'url=https://callback.domain/johnlittle/index.php' -F 'max_connections=1' -F 'allowed_updates=' -F 'certificate=@issuer.crt' 'https://api.telegram.org/bot34*****31:********************/setWebhook'

You will need to upload the public certificate used to sign your server certificate. If it is self-signed certificate, it will be the same as your server certificate. If you are using your own self-created CA, you will need to upload your self-created CA certificate.
 
Last edited:

lixiang9779

Member
Joined
Jul 19, 2009
Messages
483
Reaction score
0
Thanks!
after reading your post i have finally got a direction.
Now i know that it is not my ssl that have problem, but my code that having some issues.

Code:
$website = "https://api.telegram.org/bot".$botToken;

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

//print_r($update);

$updateArray = json_decode($update,TRUE);
//error_log($updateArray);
$chatID= $updateArray["result"][0]["message"]["chat"]["id"];


//$text = $updateArray["result"][0]["message"]["text"];

file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=HELLO!");


If i sub the $chatID with my telegram id, i am able to receive reply.
But when when i use $chatID it gives and error.

Code:
PHP Notice:  Undefined index: result in /var/www/html/telebot2.php on line 11

Code:
PHP Warning:  file_get_contents(https://api.telegram.org/bot######/sendmessage?chat_id=&text=HELLO!): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request\r\n in /var/www/html/telebot2.php on line 16
[Mon Feb 13 05:25:05.971271 20

Am i pointing it wrongly? but i am able to get a reply using the same exact code using getUpdates
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Thanks!
after reading your post i have finally got a direction.
Now i know that it is not my ssl that have problem, but my code that having some issues.

Code:
$website = "https://api.telegram.org/bot".$botToken;

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

//print_r($update);

$updateArray = json_decode($update,TRUE);
//error_log($updateArray);
$chatID= $updateArray["result"][0]["message"]["chat"]["id"];


//$text = $updateArray["result"][0]["message"]["text"];

file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=HELLO!");


If i sub the $chatID with my telegram id, i am able to receive reply.
But when when i use $chatID it gives and error.

Code:
PHP Notice:  Undefined index: result in /var/www/html/telebot2.php on line 11

Code:
PHP Warning:  file_get_contents(https://api.telegram.org/bot######/sendmessage?chat_id=&text=HELLO!): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request\r\n in /var/www/html/telebot2.php on line 16
[Mon Feb 13 05:25:05.971271 20

Am i pointing it wrongly? but i am able to get a reply using the same exact code using getUpdates

When you use print_r to print out your JSON array, did you not notice your mistake ? Are you sure there is a "result" index in the JSON array that you have decoded ?


Here is a sample
Code:
# cat index.php
<?php
$body = file_get_contents('php://input');
$o = json_decode($body, true);
error_log(print_r($o, true));
$id = $o['message']['chat']['id'];
$txt = $o['message']['text'];
$dt = date("D, d M Y H:i:s");
system("curl -F 'parse_mode=Markdown' -F 'chat_id=$id' -F 'text=You ask me *\"$txt\"* on *$dt*' 'https://api.telegram.org/bot3*****1:AAF******_YzQ/sendMessage'");
?>

Update: There is a security problem with the code above. Who wanna take a stab at it? :)
You sure you don't want to take a closer stab at it ?
Really ??? Okay say you give up! :)
Code Injection. In this case, Shell code injection
Code:
'.$(ls).'
 
Last edited:

lixiang9779

Member
Joined
Jul 19, 2009
Messages
483
Reaction score
0
Solved!!!!

Your method of parsing json is correct.

I parsed according to get updates
Which is different!!


I guess will use this thread to update my progress on making my telegram bot :)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Solved!!!!

Your method of parsing json is correct.

I parsed according to get updates
Which is different!!


I guess will use this thread to update my progress on making my telegram bot :)

My parsing of JSON is not different from yours. :) We are using the same php json decoding library. The difference is how we extract information from the associative array. You have to take note on how you are extracting information from a data structure
 

lixiang9779

Member
Joined
Jul 19, 2009
Messages
483
Reaction score
0
Update since the last post after help from davidktw

I have tried to implement a simple chatbot idea

GG5lM


EQI4NEc


My bot helps to get some price quote for SGX stocks,
also there is an alert function able to notify me when the price have reached as shown in second picture.


Current problem:
some stock quotes such as D05 is not working,
it shows fail to open stream, because the string converts & to & which makes it an invalid link in telegram.

Future idea:
Update me when there is new SGX news.



Also is there any idea for telegram bot so i can try out? :D
 
Last edited:

Asphodeli

Arch-Supremacy Member
Joined
Jul 8, 2001
Messages
23,366
Reaction score
4,308
wah u write from scratch? i went for the "fast & easy" solution which is to clone an existing bot written in python and modify it slightly for my use.
 

lixiang9779

Member
Joined
Jul 19, 2009
Messages
483
Reaction score
0
wah u write from scratch? i went for the "fast & easy" solution which is to clone an existing bot written in python and modify it slightly for my use.

Yeah.
Hybrid of php and Python.

Share you chat not please
I want to learn from you guys:)
 

lixiang9779

Member
Joined
Jul 19, 2009
Messages
483
Reaction score
0
Joined! Shall see how the bot works.
I also made some improvement to my bots:)
Hope I can learn from you
 

lixiang9779

Member
Joined
Jul 19, 2009
Messages
483
Reaction score
0
Paging for all pro programmers. I need some help.

I am trying to use to 'Switch to Inline buttons' function which i read from
https://core.telegram.org/bots/2-0-intro#switch-to-inline-buttons

but i can't seem to get it to work.

The closest i ever got was below:
HPhwCzZ


This is using the code
Code:
https://api.telegram.org/bot[API KEY]?chat_id=[CHAT_ID]&text=Hello&reply_markup={%22keyboard%22:[[%22Yes%22,%22No%22],[%22Maybe%22],[%221%22,%222%22,%223%22]],%20%22one_time_keyboard%22:true}

But i want the inline keyboard to be in the message screen and not the keyboard.


Is there any one have experience in this?
Im not using any library so i trying to find a way to do it just by file_get_content or curl
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Paging for all pro programmers. I need some help.

I am trying to use to 'Switch to Inline buttons' function which i read from
https://core.telegram.org/bots/2-0-intro#switch-to-inline-buttons

but i can't seem to get it to work.

The closest i ever got was below:
HPhwCzZ


This is using the code
Code:
https://api.telegram.org/bot[API KEY]?chat_id=[CHAT_ID]&text=Hello&reply_markup={%22keyboard%22:[[%22Yes%22,%22No%22],[%22Maybe%22],[%221%22,%222%22,%223%22]],%20%22one_time_keyboard%22:true}

But i want the inline keyboard to be in the message screen and not the keyboard.


Is there any one have experience in this?
Im not using any library so i trying to find a way to do it just by file_get_content or curl

zxp5kx.png


Code:
system("curl -F 'parse_mode=Markdown' -F 'chat_id=$id' -F 'text=Wanna visit search engines ?' -F 'reply_markup={\"inline_keyboard\":[[{\"text\":\"Google\",\"url\":\"https://www.google.com.sg\"},{\"text\":\"Yahoo\",\"url\":\"https://www.yahoo.com\"}]]}' 'https://api.telegram.org/bot*****:*****/sendMessage'");
 

lixiang9779

Member
Joined
Jul 19, 2009
Messages
483
Reaction score
0
Power !


Thank you shifu!:s13:

Got time i go make some changes so that i can implement in my telegrambot.
Although im not using it to redirect a url. most likely going to use callback function
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top