Skip to content
This repository has been archived by the owner on Feb 18, 2019. It is now read-only.

ZSTU_where_is_my_teacher_bot #1

Merged
merged 8 commits into from
May 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Commands/GenericmessageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Longman\TelegramBot\Commands\SystemCommands;

use Longman\TelegramBot\Commands\SystemCommand;
use WhereIsMyTeacherBot\Model\TeacherParser;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Request;
Expand Down Expand Up @@ -352,11 +353,14 @@ public function execute()
];

if (in_array($message->getText(), $teachers)) {
$answer = 'https://rozklad.ztu.edu.ua/schedule/teacher/' . $message->getText();

$teacherUrl = 'https://rozklad.ztu.edu.ua/schedule/teacher/' . $message->getText();

$answer = TeacherParser::parse($teacherUrl, $message->getText()) .
"\n\n<a href='$teacherUrl'>Повний розклад викладача</a>";

return Request::sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' => '<a href="' . $answer . '">' . $answer . '</a>',
'text' => $answer,
'parse_mode' => 'HTML',
]);
}
Expand All @@ -374,14 +378,15 @@ public function execute()

return Request::sendMessage($data);
}

/**
* @param array $teachers
* @param Message $message
*
* @return array|null
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private function searchTextOccurrences(array $teachers, Message $message): ?array
private function searchTextOccurrences(array $teachers, Message $message): array
{
$patterns = [];
foreach ($teachers as $teacher) {
Expand Down
54 changes: 54 additions & 0 deletions WhereIsMyTeacherBot/Model/TeacherParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);

namespace WhereIsMyTeacherBot\Model;

use phpQueryObject;

class TeacherParser
{
public static function parse(string $url, string $teacher): string
{
\phpQuery::newDocumentFileHTML($url);

$currentTable = pq('th.selected')->parents('table');
$currentWeek = $currentTable->prev()->text();

$lessons = pq('th.selected')->parents('table')->find("td:contains(\"$teacher\")");

if (!$lessons) {
return "$currentWeek для даного викладача - тиждень без пар";
}

$lessonsList = "<b>Показано $currentWeek</b>\n";

foreach (self::getGroupedLessons($lessons) as $day => $dayLessons) {
$lessonsList .= "\n<b>$day:</b>\n";
/** @var phpQueryObject $pqLesson */
foreach ($dayLessons as $time => $pqLesson) {
$lessonsList .= "\n<b>$time:</b> " . trim(str_replace($teacher, '', $pqLesson->text())) . "\n";
}
}

return $lessonsList;
}

/**
* @param \phpQueryObject $lessons
*
* @return array
*/
protected static function getGroupedLessons(phpQueryObject $lessons): array
{
$groupedLessons = [];

foreach ($lessons as $lesson) {
$pqLesson = pq($lesson);
$hours = $pqLesson->attr('hour');
$day = trim($pqLesson->attr('day'), ' 12');
$groupedLessons[$day][$hours] = $pqLesson;
}

return $groupedLessons;
}
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"type": "project",
"autoload": {
"psr-4": {
"Rozklad\\": "src/"
"WhereIsMyTeacherBot\\": "WhereIsMyTeacherBot/"
}
},
"require": {
"php": "^7.0",
"longman/telegram-bot": "^0.44.1"
"longman/telegram-bot": "^0.44.1",
"electrolinux/phpquery": "^0.9.6"
},
"require-dev": {
}
Expand Down
57 changes: 48 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 3 additions & 13 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require_once __DIR__ . '/vendor/autoload.php';

use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Telegram;

try {
Expand All @@ -12,18 +11,9 @@
'bot_name'
);
$telegram->addCommandsPath(__DIR__ . '/Commands');

// Handle telegram webhook request
$telegram->handle();

return [
'statusCode' => 200,
'body' => $telegram->getLastCommandResponse()->__toString(),
];
} catch (TelegramException $e) {

return [
'statusCode' => 500,
'body' => $e->getMessage(),
];
} catch (Exception $e) {
echo "<pre>$e</pre>";
}