I try to send a message to a telegram supergroup in two ways:
tinker(working method):
> $api = new danogMadelineProtoAPI('session.madeline');
> $api->start();
> $api->sendMessage(peer: -1002056889962, message: 'TEST test <br /> TEST test', parseMode: danogMadelineProtoParseMode::HTML, replyToMsgId: 11164, topMsgId: 11164);
Console command launched in scheduler(not working method):
<?php
namespace AppConsoleCommands;
use AppMailingStatusEnum;
use AppModelsMailing;
use AppModelsTelegramGroup;
use AppModelsTelegramUser;
use AppModelsTopic;
use danogMadelineProtoAPI;
use danogMadelineProtoLocalFile;
use danogMadelineProtoParseMode;
use IlluminateConsoleCommand;
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,
];
}
}
As a result, when sending a message through Tinker, it is sent perfectly, but if I use the console command called by the scheduler, I get the error: This peer is not present in the internal peer database
I’ve already tried a lot, although when I noticed that everything works through the tinker, but not through the scheduler, now I can’t get it out of my head and I don’t know where to go next
——-
ADDITION:
Not in all cases when working with the scheduler I get an error, but here is one of the rather strange cases that I don’t know how to solve