/** * ЦСКА */ public function actionCskanews() { $cska = Yii::$app->params['cska']; $bot = new BotApi($cska['token']); $host = 'https://www.sports.ru'; $dom = Html::load($cska['html']); $divs = $dom->getElementsByTagName('div'); foreach ($divs as $div) { if ($div->getAttribute('class') == 'news') { $links = $div->getElementsByTagName('a'); foreach ($links as $link) { if ($link->getAttribute('class') == 'short-text') { $url = Url::findOne(['channel' => 'cska', 'url' => $host . $link->getAttribute('href')]); if ($url === null) { $message = $link->nodeValue . "\n" . $host . $link->getAttribute('href'); try { $bot->sendMessage($cska['chat_id'], $message); } catch (Exception $e) { echo $e->getMessage() . "\n"; } $url = new Url(['channel' => 'cska', 'url' => $host . $link->getAttribute('href')]); $url->save(); } } } } } }
/** * Event tracking * * @param \TelegramBot\Api\Types\Message $message * @param string $eventName * * @throws \TelegramBot\Api\Exception * @throws \TelegramBot\Api\HttpException */ public function track(Message $message, $eventName = 'Message') { $uid = $message->getFrom()->getId(); $options = [CURLOPT_URL => self::BASE_URL . "?token={$this->token}&uid={$uid}&name={$eventName}", CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POSTFIELDS => $message->toJson()]; curl_setopt_array($this->curl, $options); $result = BotApi::jsonValidate(curl_exec($this->curl), true); BotApi::curlValidate($this->curl); if ($result['status'] !== 'accepted') { throw new Exception('Error Processing Request'); } }
/** * Проверка замен перед дедлайном */ public function actionCheck() { $bot = new BotApi(Yii::$app->params['token']); $time = time() + 2 * 60 * 60; $tournaments = Tournament::find()->where(['checked' => false])->andWhere(['<', 'deadline', date('Y-m-d H:i:s', $time)])->all(); foreach ($tournaments as $tournament) { foreach ($tournament->teams as $team) { if ($team->user->notification) { $transfers = $team->getTransfers(); if ($transfers == $tournament->transfers) { $message = 'Ты ещё не сделал замены, скоро дедлайн:'; $date = new DateTime($tournament->deadline, new DateTimeZone(Yii::$app->timeZone)); $message .= "\n" . $date->format('H:i') . ' ' . $tournament->name; try { $bot->sendMessage($team->user->chat_id, $message); } catch (Exception $e) { Yii::info($e->getMessage(), 'send'); } } } } $tournament->checked = true; $tournament->save(); } }
/** * Webhook handler * * @return array * @throws \TelegramBot\Api\InvalidJsonException */ public function run() { if ($data = BotApi::jsonValidate($this->getRawBody(), true)) { $this->handle([Update::fromResponse($data)]); } }
/** * Отправляет сообщение */ public function send($chatId, $message) { $bot = new BotApi(Yii::$app->params['token']); try { $bot->sendMessage($chatId, $message); } catch (Exception $e) { Yii::info(print_r($e, true), 'send'); } }
/** * Webhook handler * * @return array * @throws \TelegramBot\Api\InvalidJsonException */ public function run() { if ($data = BotApi::jsonValidate(file_get_contents('php://input'), true)) { $this->handle([Update::fromResponse($data)]); } }
public function __construct(Container $container) { $token = $container->getParameter('shaygan_telegram_bot_api.config'); // var_dump($token); parent::__construct($token['token']); }