Replies: 2 comments
-
Please update your previous posts, but not send answers to the discussion. Use threads to respond to other messages. Everything seems fine for your config, you should check if your server can reach Telegram API - watch application logs where your webhook is installed and search for CURL exceptions. Be aware that webhook don't work locally! Also, I have some recommendations to help you organize your project as well:
'webhook' => [
// 'url' => env('TELEGRAM_BOT_WEBHOOK_URL', env('APP_URL').'/telebot/webhook/bot/'.env('TELEGRAM_BOT_TOKEN')),
// 'certificate' => env('TELEGRAM_BOT_CERT_PATH', storage_path('app/ssl/public.pem')),
// 'ip_address' => '8.8.8.8',
// 'max_connections' => 40,
// 'allowed_updates' => ["message", "edited_channel_post", "callback_query"],
// 'secret_token' => env('TELEGRAM_KEY', null),
],
'poll' => [
// 'limit' => 100,
// 'timeout' => 0,
// 'allowed_updates' => ["message", "edited_channel_post", "callback_query"]
],
$this->bot->sendMessage([
'chat_id' => $chatId,
'text' => "Дякуємо вам за підключення бота, тепер ви можете отримувати повідомлення прямо в бота. \n<a href='$pageUrl'>Особистий кабінет</a>",
'parse_mode' => 'HTML',
])->wait();
// BAD
function foo($x, $y, $z)
{
if ($x) {
if ($y) {
if ($z) {
return 'bar';
} else {
return null;
}
} else {
return null;
}
} else {
return null;
}
}
// BETTER
function foo($x, $y, $z)
{
if (!$x) {
return null;
}
if (!$y) {
return null;
}
if (!$z) {
return null;
}
return 'bar';
}
// BEST
function foo($x, $y, $z)
{
if (!$x || !$y || !$z) {
return null;
}
return 'bar';
}
<?php
namespace App\Telegram\Handlers\Admar;
use App\Models\TelegramUser;
use Illuminate\Support\Facades\DB;
use WeStacks\TeleBot\Handlers\CommandHandler;
class StatAdmarBotHandler extends CommandHandler
{
protected static $aliases = ['/start'];
protected static $description = "Start the bot";
protected string $token;
protected TelegramUser $telegramUser;
public function trigger()
{
return parent::trigger() &&
($this->token = $this->arguments(1)) &&
($this->telegramUser = TelegramUser::where('token', $this->token)->first());
}
protected function arguments(?int $index = null, $default = null)
{
$arguments = explode(' ', $this->update->message()->text);
if (!is_null($index)) {
return $arguments[$index] ?? $default;
}
return $arguments;
}
public function handle()
{
// Utilize database tranzactions when updating multiple records at the same time
DB::transaction(function () {
$this->telegramUser->update([
'chatId' => $chatId = $this->update->chat()->id,
'username' => $username = $this->update->user()->username,
'firstName' => $this->update->user()->first_name ?? null,
'lastName' => $this->update->user()->last_name ?? null,
// Commented it out cos it will not even work see https://core.telegram.org/bots/api#user
// 'avatar' => isset($this->update->message()->from->photo) ? TeleBot::getFileUrl(
// $this->update->message()->from->photo->big_file_id
// ) : null
]);
$this->telegramUser->user->update([
'telegram_id' => $chatId,
'telegram' => '@' . $username,
]);
});
// You should never use `env()` function anywhere except config files inside Laravel
// $pageUrl = env('APP_URL') . '/my-account/'; // URL страницы для просмотра
$pageUrl = url('/my-account'); // URL страницы для просмотра
// Be aware, that if your handler have void return type, your update will be passed to next handler in the list
$this->sendMessage([
// Don't need to set chat id if respond to same user
// 'chat_id' => $chatId,
'text' => "Дякуємо вам за підключення бота, тепер ви можете отримувати повідомлення прямо в бота. \n<a href='$pageUrl'>Особистий кабінет</a>",
'parse_mode' => 'HTML',
]);
}
} |
Beta Was this translation helpful? Give feedback.
-
It looks like you are facing a couple of issues with your Telegram bot webhook setup. Let's address them step by step:
By following these steps, you should be able to identify and resolve the issues with your webhook setup and ensure that your Telegram bot can receive and process messages correctly. If you continue to face issues, consider enabling detailed logging in your bot to help diagnose the problem further. |
Beta Was this translation helpful? Give feedback.
-
Please help me why my webhook returns an error
Tokin is faithful.
As I understood from the library, I need to create routes and a method that will process the webhook and just execute the command
php artisan telebot:webhook --setup
?What am I doing wrong, why the webhook is not working?
UPD: The webhook address should be like this, problem solved:
UPD: Everything works, the bot returns, but why messages do not come to the bot
UPD: when i worked through polling
I have a telegram bot sending messages to a user,
Now, for some reason, when I connected the webhook, it does not send messages, although there is a return, I write data to the database.
What could be the problem ?
here is the code
Beta Was this translation helpful? Give feedback.
All reactions