Eleirbag89 / TelegramBotPHP

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

Chat id null #116

Closed fwag closed 6 years ago

fwag commented 6 years ago

I've uploaded the composer downloaded dir on my ftp repo and added the following code in a page called send_alert.php

`<?php

include (DIR . '/vendor/autoload.php');

date_default_timezone_set("Europe/Rome");

$token = "4...."; $telegram = new Telegram($token);

$chat_id = $telegram->ChatID();

var_dump($chat_id);

$result = $telegram->getData(); $text = $result['message'] ['text']; $chat_id = $result['message'] ['chat']['id']; $content = array('chat_id' => $chat_id, 'text' => 'Test'); $telegram->sendMessage($content);

?>`

then I've added the webhook with:

https://api.telegram.org/bot4161.../setWebhook?url=https://www.mywebsite.altervista.org/AlertBot/send_alert.php

but I got the error:

description: Bad Request: chat_id is empty inside TelegramErrorLogger.txt

Do you know why ?

pablosouza10 commented 6 years ago

Try include telegram.php;

fwag commented 6 years ago

I don't think depends on the include because I'm using composer and the include follows the doc. Furthermore I've tried in polling and it works.

fwag commented 6 years ago

I've been able to get the chat_id with the $telegram->getMe(). I have hardcoded the chat_id but I'm able to receive the message sent with sendMessage only if I write the "/start" command on the bot chat. Do you know why ?

Eleirbag89 commented 6 years ago

i need to see your bot script code

ATouhou commented 6 years ago

I could not use the id provided by getMe(). I dumped the error response and it was 403 "Forbidden: bot can't send messages to bots". The solution was to go to Telegram and search for the bot called "userinfobot" with the questionmark as an avatar. Click start and it gives you your userid. Use that, and you will start receiving messages from your bot :-)

Eleirbag89 commented 6 years ago

You can try to dump the $result and see if the webhook is sending something strange