VacancyChannel.php 1.23 KB
<?php

namespace App\Components\Integrations\Telegram;

use App\Models\Ad_employer;
use Str;
use Telegram\Bot\Api;
use Telegram\Bot\Exceptions\TelegramSDKException;
use Telegram\Bot\Laravel\Facades\Telegram;
use Telegram\Bot\Objects\Update;

class VacancyChannel
{
    private Api $botApi;

    public function __construct()
    {
        $this->botApi = Telegram::bot('channel_bot');
    }

    /**
     * @throws TelegramSDKException
     */
    public function sendVacancy(Ad_employer $vacancy): void
    {
        $updates = $this->botApi->getUpdates();
        /** @var Update $update*/
        $chatId = null;
        foreach ($updates as $update) {
            if ($update->myChatMember?->chat->title === config('services.telegram.chat_title')) {
                $chatId = $update->myChatMember->chat->id;
            }
        }
        if ($chatId === null) {
            throw new TelegramSDKException('Cant locate group chat id');
        }

        $text = str_ireplace('<p>', '', $vacancy->text);
        $text = str_ireplace('</p>', '', $text);

        $this->botApi->sendMessage([
            'chat_id' => $chatId,
            'text' => $text,
            'disable_web_page_preview' => true,
            'parse_mode' => 'HTML'
        ]);
    }
}