1 <?php
declare(strict_types
=1);
3 require_once('common.php');
5 use Telegram\Bot\Actions
;
6 use Telegram\Bot\Commands\Command
;
8 if(getTelegram()->getWebHookUpdates()->get('message') && !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 $message = getTelegram()->getWebHookUpdates()->get('message') ?
getTelegram()->getWebHookUpdates()->get('message')->get('text') : '';
18 if ($between = between(reveal($message ??
''), '[taskid]')) {
19 $monday = turnBackTime(new DateTimeImmutable());
20 $completedTasksFile = getFilePathForWeek($monday->year
, $monday->month
, $monday->weekNum
);
21 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
22 $tasksForTheWeek = getTasksForTheWeek(
26 require 'taskMatrix.php'
29 $chatid = getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id');
31 if (!is_dir(dirname($completedTasksFile))) {
32 mkdir(dirname($completedTasksFile), 0777, true
);
35 if (in_array($between, $completedTasks)) {
36 getTelegram()->sendMessage([
38 'text' => getString('taskAlreadyCompleted')
43 if (!in_array($between, $tasksForTheWeek)) {
44 getTelegram()->sendMessage([
46 'text' => getString('unknownTask')
57 getTelegram()->sendMessage([
59 'text' => getString('taskCompleted'),
60 'reply_markup' => json_encode(['remove_keyboard' => true
])
63 getTelegram()->sendSticker([
65 'sticker' => 'CAADBQADOwAD__7RB2i3XcCiO8HuAg'
68 if (!array_diff($tasksForTheWeek, array_merge($completedTasks, [$between]))) {
69 getTelegram()->sendMessage([
71 'text' => getString('heyTasksAllCompleted')
76 if ($between = between(reveal($message ??
''), '[billid]')) {
77 $messages = array_values(array_filter(
80 'Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'
85 function($e) use ($between) {
86 return $e['id'] == $between;
90 if(!$messages ||
count($messages) !== 1) {
91 getTelegram()->sendMessage([
92 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
93 'text' => "That doesn't look like a valid id. Use /mybills to list your bills."
98 imap_delete(getInbox('Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'), $messages[0]['uid'], FT_UID
);
99 getTelegram()->sendMessage([
100 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
101 'text' => "I marked " . getMessageSenderDisplayName(getTelegram()->getWebHookUpdates()) . " as having paid the " . strtolower($messages[0]['service']) . " bill.",
102 'reply_markup' => json_encode(['remove_keyboard' => true
])
105 getTelegram()->sendSticker([
106 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
107 'sticker' => 'CAADBQADOwAD__7RB2i3XcCiO8HuAg'
111 getTelegram()->addCommand(
112 new class extends Command
{
113 protected $name = 'searchmovies';
114 protected $description = 'Search through the movie collection';
116 public function handle($arguments) {
117 $top5 = ∘
(unlines
, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort(∘
(∘
f(field('similarity')), ⬄
, field('similarity'))));
118 $movies = map(function($movie) use ($arguments) {
119 similar_text($arguments, substr($movie, 0, -7) ?
: '', $perc);
120 return ['title' => $movie, 'similarity' => $perc];
121 })(scandir('/mnt/media/Movies'));
123 $this->replyWithMessage(
125 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies)
132 getTelegram()->addCommand(
133 new class extends Command
{
134 protected $name = 'chatid';
135 protected $description = 'Get the id for this chat.';
137 public function handle($arguments) {
138 $this->replyWithMessage([
139 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
145 getTelegram()->addCommand(
146 new class extends Command
{
147 protected $name = 'mybills';
148 protected $description = 'List my bills';
150 public function handle($arguments) {
151 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
152 $this->replyWithChatAction(['action' => Actions
::TYPING
]);
153 $this->replyWithMessage([
159 "%s: $%s each due on the %s",
161 splitBill($bill['amount']),
162 $bill['due']->format('jS \of M')
165 getMessagesFromInbox(
167 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
172 ) ?
: ['You have no unpaid bills! Nice one.']
179 getTelegram()->addCommand(
180 new class extends Command
{
181 protected $name = 'paybill';
182 protected $description = 'Mark a bill as paid';
184 public function handle($arguments) {
185 $buttons = [array_values(map(function($bill) {
186 return sprintf('%s%s', hide('[billid]' . $bill['id'] . '[billid]'), $bill['service'] . ' ($' . splitBill($bill['amount']) . ')');
187 })(getMessagesFromInbox(
189 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
196 $this->replyWithMessage([
197 'text' => getMessageSenderDisplayName($this->getUpdate()). ' doesn\'t have any outstanding bills. Nice :)',
198 'reply_markup' => json_encode(['remove_keyboard' => true
])
203 $reply_markup = getTelegram()->replyKeyboardMarkup([
204 'keyboard' => $buttons,
205 'resize_keyboard' => true
,
206 'one_time_keyboard' => true
,
210 $this->replyWithMessage(
212 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which bill did you want to pay?',
213 'parse_mode' => 'markdown',
214 'reply_markup' => $reply_markup
222 getTelegram()->addCommand(
223 new class extends Command
{
224 protected $name = 'tasks';
225 protected $description = 'List tasks for this week';
227 public function handle($arguments) {
228 $monday = turnBackTime(new DateTimeImmutable());
229 $tasksForTheWeek = getTasksForTheWeek(
233 require 'taskMatrix.php'
236 $completedTasksFile = getFilePathForWeek($monday->year
, $monday->month
, $monday->weekNum
);
237 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
239 if (!array_diff($tasksForTheWeek, $completedTasks)) {
240 $this->replyWithMessage([
241 'text' => getString('tasksAllCompleted')
246 $this->replyWithMessage([
247 'text' => ununlines([
248 getString('tasksForTheWeek'),
249 unlines(map(getString
)(array_diff($tasksForTheWeek, $completedTasks)))
256 getTelegram()->addCommand(
257 new class extends Command
{
258 protected $name = 'completetask';
259 protected $description = 'Mark a task as completed';
261 public function handle($arguments) {
262 $monday = turnBackTime(new DateTimeImmutable
);
263 $tasksForTheWeek = getTasksForTheWeek(
267 require 'taskMatrix.php'
270 $completedTasksFile = getFilePathForWeek($monday->year
, $monday->month
, $monday->weekNum
);
271 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
273 if (!array_diff($tasksForTheWeek, $completedTasks)) {
274 $this->replyWithMessage([
275 'text' => getString('tasksAllCompleted')
280 $tasks = array_values(map(function($task) {
281 return sprintf('%s%s', hide('[taskid]' . $task . '[taskid]'), getString($task));
282 })(array_diff($tasksForTheWeek, $completedTasks)));
283 $buttons = partition((int)ceil(count($tasks)/3), $tasks);
284 $reply_markup = getTelegram()->replyKeyboardMarkup([
285 'keyboard' => $buttons,
286 'resize_keyboard' => true
,
287 'one_time_keyboard' => true
,
291 $this->replyWithMessage(
293 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which task did you finish?',
294 'parse_mode' => 'markdown',
295 'reply_markup' => $reply_markup
302 getTelegram()->commandsHandler(true
); //must come afterwards because lolzer