MadelineProto调度器发送Telegram消息失败:Peer数据库异常排查
Telegram超级群消息发送异常排查:调度器命令报错"This peer is not present in the internal peer database"
正常工作的Tinker方式
$api = new danog\MadelineProto\API('session.madeline'); $api->start(); $api->sendMessage(peer: -1002056889962, message: 'TEST test <br /> TEST test', parseMode: danog\MadelineProto\ParseMode::HTML, replyToMsgId: 11164, topMsgId: 11164);
异常的调度器启动控制台命令
<?php namespace App\Console\Commands; use App\MailingStatusEnum; use App\Models\Mailing; use App\Models\TelegramGroup; use App\Models\TelegramUser; use App\Models\Topic; use danog\MadelineProto\API; use danog\MadelineProto\LocalFile; use danog\MadelineProto\ParseMode; use Illuminate\Console\Command; use Throwable; class SendMailingsCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'app:send-mailings-command'; /** * The console command description. * * @var string */ protected $description = 'check and send mailings'; /** * Execute the console command. */ public function handle(): void { $lockFile = '/tmp/telegram_bot_mailing.lock'; $hour = now()->hour; $minute = now()->minute; $second = now()->second; $startTime = "startTime $hour:$minute:$second:"; if (file_exists($lockFile)) { exit(); } file_put_contents($lockFile, 'running'); if (Mailing::query()->where('status', MailingStatusEnum::PROCESSING)->count() > 0) { return; } $madelineProto = new API('session.madeline'); $mailing = Mailing::query() ->where('status', MailingStatusEnum::NEW) ->first(); if (!$mailing) { return; } try { $mailing->update(['status' => MailingStatusEnum::PROCESSING]); foreach ($mailing->telegram_group_ids as $telegramGroupId) { $group = TelegramGroup::query()->find($telegramGroupId); $text = str_replace(' ', ' ', $mailing->text) . '<br /><br />'; $group ->usersWithRoles() ->whereIn('role_id', $mailing->role_ids) ->each(function (TelegramUser $telegramUser) use (&$text) { $text .= $telegramUser->telegram_tag ? ' @'.$telegramUser->telegram_tag : " <a href='tg://user?id={$telegramUser->id}'>$telegramUser->name</a>"; }); $media = []; $mediaFiles = $mailing->getMedia(); foreach ($mediaFiles as $index => $mediaFile) { if ($file = $this->handleFile($mediaFile, $index, $text, $mediaFiles->count())) { logger('file', [$file]); $media[] = $file; } } $mailing->update([ 'media_files' => $media ]); $resultMailing = null; $group ->topics() ->whereIn('name', $mailing->topic_ids) ->each(function(Topic $topic) use ($media, $madelineProto, $group, $text, $startTime) { $resultMailing = count($media) === 0 ? $madelineProto ->sendMessage( peer: $group->telegram_id, message: $text, parseMode: ParseMode::HTML, replyToMsgId: $topic->topic_id, topMsgId: $topic->topic_id, ) : $madelineProto ->messages ->sendMultiMedia( peer: $group->telegram_id, reply_to_msg_id: $topic->topic_id, top_msg_id: $topic->topic_id, multi_media: $media, ); logger() ->channel('mailing_sending_log') ->info("$startTime sending mailing", ['resultMailing' => $resultMailing, 'group' => $group]); }); } $mailing->update(['status' => MailingStatusEnum::SENDED]); } catch (Throwable $e) { logger() ->channel('mailing_sending_log') ->info("$startTime ошибка рассылки", ['error' => $e, 'mailing' => $mailing]); $mailing->update([ 'status' => MailingStatusEnum::FAILED, 'error' => [$e], ]); } unlink($lockFile); } protected function handleFile($mediaFile, int $index, string $text, $count): array|null { $filePath = $mediaFile->getPath(); $mimeType = mime_content_type($filePath); return [ '_' => 'inputSingleMedia', 'media' => [ '_' => 'inputMediaUploadedDocument', 'force_file' => true, 'file' => new LocalFile($filePath), 'mime_type' => $mimeType, 'attributes' => [ ['_' => 'documentAttributeFilename', 'file_name' => basename($filePath)] ] ], 'message' => $index === $count - 1 ? $text : '', 'parse_mode' => ParseMode::HTML, ]; } }
报错信息
触发异常:This peer is not present in the internal peer database
问题说明
通过Tinker发送消息可正常完成,但使用Laravel调度器调用上述控制台命令时触发上述报错。已尝试多种方法,仅Tinker方式有效,调度器执行场景偶发该异常,目前无法定位问题,寻求技术帮助。
内容的提问来源于stack exchange,提问作者Vladislav
相关产品推荐
相关产品推荐

