function webhook() { $telegram = app('telegram')->bot(); $message = $telegram->getWebhookUpdates()->getMessage(); if (!$message || !$message->getFrom()) { return response('OK'); } AsukaDB::createOrUpdateUser($message->getFrom()); if (Helpers::isGroup($message->getChat())) { // Store this group if it's a new group or the title was updated if ($message->getGroupChatCreated() || $message->getSupergroupChatCreated() || $message->getNewChatParticipant()) { AsukaDB::createOrUpdateGroup($message->getChat()); } if ($message->getNewChatTitle()) { AsukaDB::updateGroup($message->getChat()); } // Check if this group is authorised to use the bot if (!Helpers::groupIsAuthorized($message->getChat())) { return response('OK'); } } if (AsukaDB::getUser($message->getFrom()->getId())->ignored) { return response('OK'); } $telegram->commandsHandler(true); return response('OK'); }
protected function reply($response, $params = []) { if (empty($response)) { Log::error('Tried to send an empty message...', [$this->update->getMessage(), $response]); return; } if (!array_key_exists('reply_to_message_id', $params)) { $params['reply_to_message_id'] = $this->update->getMessage()->getMessageId(); } Helpers::sendMessage($response, $this->getUpdate()->getMessage()->getChat()->getId(), $params); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $botKey = null; if ($request->route()[2]['botKey']) { $botKey = $request->route()[2]['botKey']; } $telegram = app('telegram'); if (!$botKey || $botKey != $telegram->getBotConfig(config('telegram.default'))['token']) { throw new NotFoundHttpException(); } if ($request->getMethod() != Request::METHOD_POST) { $ownerId = $telegram->getBotConfig(config('telegram.default'))['owner_id']; if ($ownerId) { Helpers::sendMessage(sprintf('The IP %s just accessed %s', $request->getClientIp(), $request->url()), $ownerId); } } return $next($request); }
/** * Creates a new quote from a Message containing another Message as a reply. * * @param Message $message The Message to make a quote from. Assumed to contain a valid reply. * @return int|null Qoute ID on success, null on failure. */ public static function createQuote(Message $message) { $db = app('db')->connection()->table('quotes'); $quoteSource = $message->getReplyToMessage(); $messageId = $message->getReplyToMessage()->getMessageId(); $groupId = $quoteSource->getChat()->getId(); $comment = trim(mb_strstr($message->getText(), ' ')) ?: null; self::createOrUpdateUser($quoteSource->getFrom()); $values = ['added_by_id' => $message->getFrom()->getId(), 'user_id' => $quoteSource->getFrom()->getId(), 'group_id' => $groupId, 'message_id' => $messageId, 'message_timestamp' => $quoteSource->getDate(), 'content' => $quoteSource->getText(), 'comment' => $comment]; $existing = $db->where('message_id', $messageId)->where('group_id', $groupId)->limit(1)->value('id'); if (!$existing) { return $db->insertGetId($values); } else { Helpers::sendMessage(sprintf('I already have that quote saved as #%s.', $existing), $groupId, ['reply_to_message_id' => $message->getMessageId()]); return null; } }
public function handle($arguments) { $message = $this->getUpdate()->getMessage(); // Detect a reply and add it as a quote $quoteSource = $message->getReplyToMessage(); if ($quoteSource) { if (!Helpers::isGroup($message->getChat())) { $this->reply('You can only add quotes in a group.'); return; } if (Helpers::userIsMe($quoteSource->getFrom())) { $this->reply('You cannot quote me >:)'); return; } if (Helpers::isCommand($quoteSource)) { $this->reply('Don\'t be silly, why would you quote commands?'); return; } if (Helpers::usersAreSame($message->getFrom(), $quoteSource->getFrom())) { $this->reply('Why would you quote yourself? What are you, some kind of loner?'); return; } if (!$message->isType('text')) { $this->reply(sprintf('I cannot quote %s messages, please send me a text message.', $message->detectType())); return; } $result = AsukaDB::createQuote($message); if ($result) { $this->reply(sprintf('Quote saved as #%d', $result), ['reply_to_message_id' => $quoteSource->getMessageId()]); } return; } if ($arguments) { $arguments = explode(' ', $arguments); $quoteId = intval(preg_replace('/[^0-9]/', '', $arguments[0])); if (!$quoteId) { $this->reply('Please supply a numeric quote ID.'); return; } $quote = AsukaDB::getQuote($quoteId); } else { // Random quote $quote = AsukaDB::getQuote(); } if (!$quote) { $quoteCount = app('db')->connection()->table('quotes')->count() + 1; /* Since when were SQL COUNTs 0-indexed? */ if ($quoteCount === 0) { $this->reply('I don\'t have any quotes saved yet!'); } elseif (isset($quoteId) && ($quoteId < 1 || $quoteId > $quoteCount)) { $this->reply(sprintf('Invalid quote ID. Valid values are between 1 and %d.', $quoteCount)); } else { $this->reply('No quote found!'); } return; } $response = sprintf('<b>%s</b>' . PHP_EOL, Helpers::escapeMarkdown($quote->content)); $quotee = AsukaDB::getUser($quote->user_id); $quoter = AsukaDB::getUser($quote->added_by_id); $citation = $quotee->first_name; if ($quotee->last_name) { $citation .= sprintf(' %s', $quotee->last_name); } if ($quotee->username) { $citation .= sprintf(' (%s)', $quotee->username); } $response .= sprintf('-- <i>%s, %s (#%d)</i>' . PHP_EOL, Helpers::escapeMarkdown($citation), date('D, jS M Y H:i:s T', $quote->message_timestamp), $quote->id); $addedBy = $quoter->first_name; if ($quoter->last_name) { $addedBy .= sprintf(' %s', $quoter->last_name); } if ($quoter->username) { $addedBy .= sprintf(' (%s)', $quoter->username); } $response .= sprintf(PHP_EOL . 'Added by %s' . PHP_EOL, Helpers::escapeMarkdown($addedBy)); if ($quote->comment) { $response .= sprintf('Comment: %s', Helpers::escapeMarkdown($quote->comment)); } $this->reply($response, ['disable_web_page_preview' => true, 'parse_mode' => 'HTML']); }