1 <?php
declare(strict_types
=1);
3 require_once('common.php');
5 use Telegram\Bot\Actions
;
6 use Telegram\Bot\Commands\Command
;
8 if(!canChatWith(getTelegram()->getWebhookUpdates())) {
9 getTelegram()->sendMessage([
10 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
11 'text' => "Sorry, Dad says I can't talk to you."
16 getTelegram()->addCommand(
17 new class extends Command
{
18 protected $name = 'searchmovies';
19 protected $description = 'Search through the movie collection';
21 public function handle($arguments) {
22 $top5 = ∘
(unlines
, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort(∘
(∘
f(field('similarity')), ⬄
, field('similarity'))));
23 $movies = map(function($movie) use ($arguments) {
24 similar_text($arguments, substr($movie, 0, -7) ?
: '', $perc);
25 return ['title' => $movie, 'similarity' => $perc];
26 })(scandir('/mnt/media/Movies'));
28 $this->replyWithMessage(
30 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies)
37 getTelegram()->addCommand(
38 new class extends Command
{
39 protected $name = 'chatid';
40 protected $description = 'Get the id for this chat.';
42 public function handle($arguments) {
43 $this->replyWithMessage([
44 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
50 getTelegram()->addCommand(
51 new class extends Command
{
52 protected $name = 'mybills';
53 protected $description = 'List my bills';
55 public function handle($arguments) {
56 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
57 $this->replyWithChatAction(['action' => Actions
::TYPING
]);
58 $this->replyWithMessage([
64 "%s (%s): $%s each due on the %s",
67 splitBill($bill['amount']),
68 $bill['due']->format('jS \of M')
73 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
78 ) ?
: ['You have no unpaid bills! Nice one.']
85 getTelegram()->addCommand(
86 new class extends Command
{
87 protected $name = 'paybill';
88 protected $description = 'Mark a bill as paid';
90 public function handle($arguments) {
92 $this->replyWithMessage(['text' => "I need the bill id. The /mybills command lists all your bills with the ids in brackets. Here, I'll show you..."]);
93 $this->triggerCommand('mybills');
96 $messages = array_values(array_filter(
99 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
104 function($e) use ($arguments) {
105 return $e['id'] == $arguments;
109 if(!$messages ||
count($messages) !== 1) {
110 $this->replyWithMessage(['text' => "That doesn't look like a valid id. Use /mybills to list your bills."]);
114 imap_delete(getInbox('Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'), $messages[0]['uid'], FT_UID
);
115 $this->replyWithMessage(['text' => "I marked " . getMessageSenderDisplayName($this->getUpdate()) . " as having paid the " . strtolower($messages[0]['service']) . " bill, thanks!"]);
121 getTelegram()->addCommand(
122 new class extends Command
{
123 protected $name = 'tasks';
124 protected $description = 'List tasks for this week';
126 public function handle($arguments) {
127 $dt = new DateTimeImmutable();
128 $directory = sprintf(
136 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
137 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
138 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
139 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
141 $currentMonth = (int)(new DateTimeImmutable())->format('m');
142 $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
143 $currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
144 $currentYear = (int)(new DateTimeImmutable())->format('Y');
145 $tasksForTheWeek = getTasksForTheWeek(
147 (int)$dt->format('m'),
148 require 'taskMatrix.php'
151 $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
152 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
154 $this->replyWithMessage([
155 'text' => ununlines([
156 getString('tasksForTheWeek'),
157 unlines(map(getStringAndCode
)(array_diff($tasksForTheWeek, $completedTasks)))
164 getTelegram()->addCommand(
165 new class extends Command
{
166 protected $name = 'completetask';
167 protected $description = 'Mark a task as completed';
169 public function handle($arguments) {
170 $dt = new DateTimeImmutable();
172 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
173 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
174 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
175 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
177 $closestMonday = closest($dt->format('d'), $mondays);
179 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays));
181 $tasksForTheWeek = getTasksForTheWeek(
182 array_search($closestMonday, $mondays),
183 (int)$dt->format('m'),
184 require 'taskMatrix.php'
187 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
189 if (!is_dir(dirname($completedTasksFile))) {
190 mkdir(dirname($completedTasksFile), 0777, true
);
193 if (in_array($arguments, $completedTasks)) {
194 $this->replyWithMessage(['text' => getString('taskAlreadyCompleted')]);
198 if (!in_array($arguments, $tasksForTheWeek)) {
199 $this->replyWithMessage(['text' => getString('unknownTask')]);
209 $this->replyWithMessage(['text' => getString('taskCompleted')]);
214 getTelegram()->commandsHandler(true
); //must come afterwards because lolzer