I’ve made a simple Telegram bot that invites a user to a Trello board and notifies all users in the group chat when a card is moved from one list to another. I’m facing a problem. Every time I launch Telegram and select the group chat, I get this. In a private chat between me and the bot, everything is fine.
This is the GitHub repo of my project: https://github.com/loglinn05/loglinn05_chatos_bot/
To handle webhook requests from Telegram, I use defstudio/telegraph. This is what my handler class looks like:
<?php
namespace AppTelegram;
use AppModelsUser;
use DefStudioTelegraphEnumsChatActions;
use DefStudioTelegraphHandlersWebhookHandler;
use GuzzleHttpClient;
class Handler extends WebhookHandler
{
private $trelloApiBaseUrl;
private $trelloApiKey;
private $trelloApiToken;
private $boardId;
public function __construct()
{
$this->trelloApiBaseUrl = env("TRELLO_API_BASE_URL");
$this->trelloApiKey = env("TRELLO_API_KEY");
$this->trelloApiToken = env("TRELLO_API_TOKEN");
$this->boardId = env("BOARD_ID");
}
public function start()
{
$from = $this->message->from();
$fromUser = User::where('telegram_user_id', $from->id())->first();
$this->chat->action(ChatActions::TYPING)->send();
// saying hello
$name = $from->firstName();
$this->reply("Hi, $name!");
// adding a user to the database if they're not there
if (!$fromUser) {
$user = new User();
$user->telegram_user_id = $from->id();
$user->first_name = $from->firstName();
if ($from->lastName()) {
$user->last_name = $from->lastName();
}
$user->telegram_username = $from->username();
$user->save();
if ($user) {
$this->chat->action(ChatActions::TYPING)->send();
$this->reply("You've been added to the database.");
}
}
$this->chat->action(ChatActions::TYPING)->send();
$this->reply("If you need further assistance, enter /help.");
}
public function help()
{
$this->chat->action(ChatActions::TYPING)->send();
$this->reply("
I can invite you to our Trello board. Let's work _together_! xE2x9CxA8
n*Enter /invite to proceed.*
");
}
public function invite()
{
$from = $this->message->from();
$fromUser = User::where('telegram_user_id', $from->id())->first();
$fromUser->status = "providing email";
$fromUser->save();
$this->forceToEnterEmail();
}
public function handleUnknownCommand(IlluminateSupportStringable $text): void
{
$this->chat->action(ChatActions::TYPING)->send();
$this->reply("What do you mean?
nI don't know this command! xF0x9Fx98x85");
}
public function handleChatMessage($email): void
{
$from = $this->message->from();
$fromUser = User::where('telegram_user_id', $from->id())->first();
if ($fromUser->status == "providing email") {
$fullName = [];
// set the user's email if it's not set
// and set the full name for the invitation request
if ($fromUser) {
$fullName = ['fullName' => "{$fromUser->first_name} {$fromUser->last_name}"];
}
$invitationResponse = $this->sendInvitationRequest($email, $fullName);
if (
$invitationResponse->getStatusCode() == 200
) {
if (!$fromUser->email) {
$fromUser->email = $email;
$fromUser->save();
}
$this->onSuccessfulInvitation();
} elseif (
$invitationResponse->getBody() == 'Member already invited'
) {
$this->clearFromUserStatus();
$this->chat->action(ChatActions::TYPING)->send();
$this->reply("You're already invited. Enjoy the experience!");
$this->getLinkToTheBoard();
} elseif (
json_decode($invitationResponse->getBody())->message == "invalid email address"
) {
$this->chat->action(ChatActions::TYPING)->send();
$this->reply("Invalid email address.");
$this->forceToEnterEmail();
} else {
$this->clearFromUserStatus();
$this->chat->action(ChatActions::TYPING)->send();
$this->reply("Unknown error occurred during invitation attempt.");
}
}
}
private function forceToEnterEmail()
{
$this->chat->action(ChatActions::TYPING)->send();
$this->chat->message("Please, enter your email so I can invite you:")->forceReply("Enter your email here...", selective: true)->send();
}
private function sendInvitationRequest($email, $fullName)
{
$client = new Client();
$invitationResponse = $client->put(
"$this->trelloApiBaseUrl/boards/$this->boardId/members?email=$email&key=$this->trelloApiKey&token=$this->trelloApiToken",
[
'http_errors' => false,
'json' => $fullName
]
);
return $invitationResponse;
}
private function clearFromUserStatus()
{
$from = $this->message->from();
$fromUser = User::where('telegram_user_id', $from->id())->first();
$fromUser->status = null;
$fromUser->save();
}
private function onSuccessfulInvitation()
{
$this->clearFromUserStatus();
$this->chat->action(ChatActions::TYPING)->send();
$this->reply("You've been successfully invited.");
$this->getLinkToTheBoard();
}
private function getLinkToTheBoard()
{
$client = new Client();
$getBoardResponse = $client->get("$this->trelloApiBaseUrl/boards/$this->boardId?key=$this->trelloApiKey&token=$this->trelloApiToken");
if ($getBoardResponse->getStatusCode() == 200) {
$linkToTheBoard = json_decode($getBoardResponse->getBody())->url;
$this->chat->action(ChatActions::TYPING)->send();
$this->reply("Here's the link to the board:
n$linkToTheBoard");
} else {
$this->chat->action(ChatActions::TYPING)->send();
$this->reply("Unfortunately, we couldn't get the link to the board.");
}
}
}
As you can see, the selective
field is set to true
:
private function forceToEnterEmail()
{
$this->chat->action(ChatActions::TYPING)->send();
$this->chat->message("Please, enter your email so I can invite you:")->forceReply("Enter your email here...", selective: true)->send();
}
But the bot still forces a user to enter their email.
So I decided to write something to the log in this function. And I noticed that nothing was added into the log file when I started Telegram again. In a PowerShell terminal where ngrok
was running, I didn’t notice any requests to the Telegram API when the app launches.
If you have any ideas on how to solve this issue, please, share them. I’ll appreciate any help.
If you need something else to understand my question, feel free to ask.