core: Problem with cron and runCommands

Required Information

  • Operating system: CentOS 7.3.1611
  • PHP version: 7.1.2 (64bit); apache2handler; Linux
  • PHP Telegram Bot version: 0.46.0
  • Using MySQL database: yes
  • MySQL version: 5.5.52-MariaDB
  • Update Method: Webhook
  • Self-signed certificate: no
  • RAW update (if available): Longman\TelegramBot\Entities\ServerResponse::__set_state(array( 'ok' => false, 'error_code' => 403, 'description' => 'Forbidden: bot can\'t send messages to bots', 'raw_data' => array ( 'ok' => false, 'error_code' => 403, 'description' => 'Forbidden: bot can\'t send messages to bots', ), 'bot_username' => '%mybotname%', )),

Steps to reproduce

This error produce when execute cron.php from example repository. I tried truncate users and chat tables, but no effects.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (4 by maintainers)

Most upvoted comments

/show commands gets triggered by user manually, so there is incoming update containing his user_id so the bot knows who reply to.

What you want to do is write seperate command that iterates over all users in the database, checks if they got notification and then sends a message to them using user_id from the database row.

Best thing would be to extract the /show functionality to a separate class that you can call anywhere. Then, in your cron, simply loop the user list and do a Request::sendMessage(...) for each user that has notifications. I think it doesn’t make sense to create fake updates for all of these messages.

On the topic, just use -1 for update_id, otherwise there will probably be a problem with duplicate entries at some point.

@catsAND You must understand cron is meant for commands doing some kind of scheduled tasks. Commands run with runCommands() are executed as the bot itself, and because bot cannot message itself it will return errors.

In this case you replace sendMessage() part with:

if($this->getTelegram()->getBotId() != $chat_id) {
     return Request::emptyResponse();
} else {
     return Request::sendMessage($data);
}

(the code you pasted earlier does totally different thing, it makes it so that command only answers to bot)