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 // The actual date/time when the script is called
20 $currentYear = (int)(new DateTimeImmutable())->format('Y');
21 $currentMonth = (int)(new DateTimeImmutable())->format('n');
22 $currentDay = (int)(new DateTimeImmutable())->format('d');
24 // The years/months/weeks that we are "rewinding" to, for calculations
25 $yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
26 $monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
27 $seasonForThisWeek = getSeason($monthForThisWeek);
28 $weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
30 $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
31 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
32 $tasksForTheWeek = getTasksForTheWeek(
36 require 'taskMatrix.php'
39 $chatid = getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id');
41 if (!is_dir(dirname($completedTasksFile))) {
42 mkdir(dirname($completedTasksFile), 0777, true
);
45 if (in_array($between, $completedTasks)) {
46 getTelegram()->sendMessage([
48 'text' => getString('taskAlreadyCompleted')
53 if (!in_array($between, $tasksForTheWeek)) {
54 getTelegram()->sendMessage([
56 'text' => getString('unknownTask')
67 getTelegram()->sendMessage([
69 'text' => getString('taskCompleted'),
70 'reply_markup' => json_encode(['remove_keyboard' => true
])
73 getTelegram()->sendSticker([
75 'sticker' => 'CAADBQADOwAD__7RB2i3XcCiO8HuAg'
78 if (!array_diff($tasksForTheWeek, array_merge($completedTasks, [$between]))) {
79 getTelegram()->sendMessage([
81 'text' => getString('heyTasksAllCompleted')
86 if ($between = between(reveal($message ??
''), '[billid]')) {
87 $messages = array_values(array_filter(
90 'Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'
95 function($e) use ($between) {
96 return $e['id'] == $between;
100 if(!$messages ||
count($messages) !== 1) {
101 getTelegram()->sendMessage([
102 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
103 'text' => "That doesn't look like a valid id. Use /mybills to list your bills."
108 imap_delete(getInbox('Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'), $messages[0]['uid'], FT_UID
);
109 getTelegram()->sendMessage([
110 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
111 'text' => "I marked " . getMessageSenderDisplayName(getTelegram()->getWebHookUpdates()) . " as having paid the " . strtolower($messages[0]['service']) . " bill.",
112 'reply_markup' => json_encode(['remove_keyboard' => true
])
115 getTelegram()->sendSticker([
116 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
117 'sticker' => 'CAADBQADOwAD__7RB2i3XcCiO8HuAg'
121 getTelegram()->addCommand(
122 new class extends Command
{
123 protected $name = 'searchmovies';
124 protected $description = 'Search through the movie collection';
126 public function handle($arguments) {
127 $top5 = ∘
(unlines
, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort(∘
(∘
f(field('similarity')), ⬄
, field('similarity'))));
128 $movies = map(function($movie) use ($arguments) {
129 similar_text($arguments, substr($movie, 0, -7) ?
: '', $perc);
130 return ['title' => $movie, 'similarity' => $perc];
131 })(scandir('/mnt/media/Movies'));
133 $this->replyWithMessage(
135 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies)
142 getTelegram()->addCommand(
143 new class extends Command
{
144 protected $name = 'chatid';
145 protected $description = 'Get the id for this chat.';
147 public function handle($arguments) {
148 $this->replyWithMessage([
149 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
155 getTelegram()->addCommand(
156 new class extends Command
{
157 protected $name = 'mybills';
158 protected $description = 'List my bills';
160 public function handle($arguments) {
161 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
162 $this->replyWithChatAction(['action' => Actions
::TYPING
]);
163 $this->replyWithMessage([
169 "%s: $%s each due on the %s",
171 splitBill($bill['amount']),
172 $bill['due']->format('jS \of M')
175 getMessagesFromInbox(
177 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
182 ) ?
: ['You have no unpaid bills! Nice one.']
189 getTelegram()->addCommand(
190 new class extends Command
{
191 protected $name = 'paybill';
192 protected $description = 'Mark a bill as paid';
194 public function handle($arguments) {
195 $buttons = [array_values(map(function($bill) {
196 return sprintf('%s%s', hide('[billid]' . $bill['id'] . '[billid]'), $bill['service'] . ' ($' . splitBill($bill['amount']) . ')');
197 })(getMessagesFromInbox(
199 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
206 $this->replyWithMessage([
207 'text' => getMessageSenderDisplayName($this->getUpdate()). ' doesn\'t have any outstanding bills. Nice :)',
208 'reply_markup' => json_encode(['remove_keyboard' => true
])
213 $reply_markup = getTelegram()->replyKeyboardMarkup([
214 'keyboard' => $buttons,
215 'resize_keyboard' => true
,
216 'one_time_keyboard' => true
,
220 $this->replyWithMessage(
222 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which bill did you want to pay?',
223 'parse_mode' => 'markdown',
224 'reply_markup' => $reply_markup
232 getTelegram()->addCommand(
233 new class extends Command
{
234 protected $name = 'tasks';
235 protected $description = 'List tasks for this week';
237 public function handle($arguments) {
238 // The actual date/time when the script is called
239 $currentYear = (int)(new DateTimeImmutable())->format('Y');
240 $currentMonth = (int)(new DateTimeImmutable())->format('n');
241 $currentDay = (int)(new DateTimeImmutable())->format('d');
243 // The years/months/weeks that we are "rewinding" to, for calculations
244 $yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
245 $monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
246 $seasonForThisWeek = getSeason($monthForThisWeek);
247 $weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
249 $tasksForTheWeek = getTasksForTheWeek(
253 require 'taskMatrix.php'
256 $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
257 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
259 if (!array_diff($tasksForTheWeek, $completedTasks)) {
260 $this->replyWithMessage([
261 'text' => getString('tasksAllCompleted')
266 $this->replyWithMessage([
267 'text' => ununlines([
268 getString('tasksForTheWeek'),
269 unlines(map(getString
)(array_diff($tasksForTheWeek, $completedTasks)))
276 getTelegram()->addCommand(
277 new class extends Command
{
278 protected $name = 'completetask';
279 protected $description = 'Mark a task as completed';
281 public function handle($arguments) {
282 // The actual date/time when the script is called
283 $currentYear = (int)(new DateTimeImmutable())->format('Y');
284 $currentMonth = (int)(new DateTimeImmutable())->format('n');
285 $currentDay = (int)(new DateTimeImmutable())->format('d');
287 // The years/months/weeks that we are "rewinding" to, for calculations
288 $yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
289 $monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
290 $seasonForThisWeek = getSeason($monthForThisWeek);
291 $weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
293 $tasksForTheWeek = getTasksForTheWeek(
297 require 'taskMatrix.php'
300 $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
301 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
303 if (!array_diff($tasksForTheWeek, $completedTasks)) {
304 $this->replyWithMessage([
305 'text' => getString('tasksAllCompleted')
310 $tasks = array_values(map(function($task) {
311 return sprintf('%s%s', hide('[taskid]' . $task . '[taskid]'), getString($task));
312 })(array_diff($tasksForTheWeek, $completedTasks)));
313 $buttons = partition((int)ceil(count($tasks)/3), $tasks);
314 $reply_markup = getTelegram()->replyKeyboardMarkup([
315 'keyboard' => $buttons,
316 'resize_keyboard' => true
,
317 'one_time_keyboard' => true
,
321 $this->replyWithMessage(
323 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which task did you finish?',
324 'parse_mode' => 'markdown',
325 'reply_markup' => $reply_markup
332 getTelegram()->commandsHandler(true
); //must come afterwards because lolzer