Eleirbag89 / TelegramBotPHP

A very simple PHP Telegram Bot API for sending messages.
http://eleirbag89.github.io/TelegramBotPHP
MIT License
809 stars 341 forks source link

Error with basic code...chat_id missing... #131

Closed andrebruton closed 6 years ago

andrebruton commented 6 years ago

I'm trying to get a basic example running. I've registered an app and got the app ID. I installed your library using Composer. My code is:

<?php
include (__DIR__ . '/vendor/autoload.php');
$telegram = new Telegram('519...0:BAE...n0W4');
$chat_id = $telegram->ChatID();

var_dump($chat_id);

$content = array('chat_id' => $chat_id, 'text' => 'Test 02');
$telegram->sendMessage($content);
?>

The log files give the following reply:

============[Date]============ [ 2018-02-05 13:54:31 Europe/Berlin ] ==========[Response]========== ok: False error_code: 400 description: Bad Request: chat_id is empty =========[Sent Data]========== [ref] ref.chat_id= ref.text= Test 02

============[Trace]===========

0 C:\xampp\htdocs\vdw_telegram\vendor\eleirbag89\telegrambotphp\Telegram.php(3057): TelegramErrorLogger::log(Array, Array)

1 C:\xampp\htdocs\vdw_telegram\vendor\eleirbag89\telegrambotphp\Telegram.php(93): Telegram->sendAPIRequest('https://api.tel...', Array)

2 C:\xampp\htdocs\vdw_telegram\vendor\eleirbag89\telegrambotphp\Telegram.php(178): Telegram->endpoint('sendMessage', Array)

3 C:\xampp\htdocs\vdw_telegram\test02.php(19): Telegram->sendMessage(Array)

4 {main}

Any idea why I can't get a chat ID?

NextGenZ commented 6 years ago

@andrebruton You didn't specify an chat ID

andrebruton commented 6 years ago

The $chat_id comes from this line of code: $chat_id = $telegram->ChatID();

What should the example code be then? Where do I specify the $chat_id

usernein commented 6 years ago

Did you tested the bot? Send any message to it and see if it responds If not, set $chat_id as your own id and test again

andrebruton commented 6 years ago

Ok, I've figured it out. You have to save the $chat_id's of the people subscribing to your chat via the callback / getdata routine. There is no other way of finding peoples ID unless they send it to you after visiting bot @userinfobot. You then have to use this saved ID to send your messages to.

NextGenZ commented 6 years ago

@andrebruton u can get the ID by going to this link: https://api.telegram.org/bot(Token)/getUpdates