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 $message = getTelegram()->getWebHookUpdates()->get('message')->get('text');
18 function getBetween($content,$start){
19 $r = explode($start, $content);
21 $r = explode($start, $r[1]);
27 if ($between = getBetween(reveal($message), '[billid]')) {
28 $messages = array_values(array_filter(
31 'Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'
36 function($e) use ($between) {
37 return $e['id'] == $between;
41 if(!$messages ||
count($messages) !== 1) {
42 getTelegram()->sendMessage([
43 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
44 'text' => "That doesn't look like a valid id. Use /mybills to list your bills."
49 imap_delete(getInbox('Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'), $messages[0]['uid'], FT_UID
);
50 getTelegram()->sendMessage([
51 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
52 'text' => "I marked " . getMessageSenderDisplayName(getTelegram()->getWebHookUpdates()) . " as having paid the " . strtolower($messages[0]['service']) . " bill, thanks!"
56 getTelegram()->addCommand(
57 new class extends Command
{
58 protected $name = 'searchmovies';
59 protected $description = 'Search through the movie collection';
61 public function handle($arguments) {
62 $top5 = ∘
(unlines
, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort(∘
(∘
f(field('similarity')), ⬄
, field('similarity'))));
63 $movies = map(function($movie) use ($arguments) {
64 similar_text($arguments, substr($movie, 0, -7) ?
: '', $perc);
65 return ['title' => $movie, 'similarity' => $perc];
66 })(scandir('/mnt/media/Movies'));
68 $this->replyWithMessage(
70 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies)
77 getTelegram()->addCommand(
78 new class extends Command
{
79 protected $name = 'chatid';
80 protected $description = 'Get the id for this chat.';
82 public function handle($arguments) {
83 $this->replyWithMessage([
84 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
90 getTelegram()->addCommand(
91 new class extends Command
{
92 protected $name = 'mybills';
93 protected $description = 'List my bills';
95 public function handle($arguments) {
96 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
97 $this->replyWithChatAction(['action' => Actions
::TYPING
]);
98 $this->replyWithMessage([
104 "%s: $%s each due on the %s",
106 splitBill($bill['amount']),
107 $bill['due']->format('jS \of M')
110 getMessagesFromInbox(
112 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
117 ) ?
: ['You have no unpaid bills! Nice one.']
124 getTelegram()->addCommand(
125 new class extends Command
{
126 protected $name = 'paybill';
127 protected $description = 'Mark a bill as paid';
129 public function handle($arguments) {
131 $buttons = [array_values(map(function($bill) {
132 return sprintf('%s%s', hide('[billid]' . $bill['id'] . '[billid]'), $bill['service'] . ' ($' . splitBill($bill['amount']) . ')');
133 })(getMessagesFromInbox(
135 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
141 $reply_markup = getTelegram()->replyKeyboardMarkup([
142 'keyboard' => $buttons,
143 'resize_keyboard' => true
,
144 'one_time_keyboard' => true
,
145 'remove_keyboard' => true
,
149 $this->replyWithMessage(
151 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSender($this->getUpdate()) . '), which bill did you want to pay?',
152 'parse_mode' => 'markdown',
153 'reply_markup' => $reply_markup
162 getTelegram()->addCommand(
163 new class extends Command
{
164 protected $name = 'tasks';
165 protected $description = 'List tasks for this week';
167 public function handle($arguments) {
168 $dt = new DateTimeImmutable();
169 $directory = sprintf(
177 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
178 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
179 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
180 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
182 $currentMonth = (int)(new DateTimeImmutable())->format('m');
183 $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
184 $currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
185 $currentYear = (int)(new DateTimeImmutable())->format('Y');
186 $tasksForTheWeek = getTasksForTheWeek(
188 (int)$dt->format('m'),
189 require 'taskMatrix.php'
192 $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
193 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
195 $this->replyWithMessage([
196 'text' => ununlines([
197 getString('tasksForTheWeek'),
198 unlines(map(getStringAndCode
)(array_diff($tasksForTheWeek, $completedTasks)))
205 getTelegram()->addCommand(
206 new class extends Command
{
207 protected $name = 'completetask';
208 protected $description = 'Mark a task as completed';
210 public function handle($arguments) {
211 $dt = new DateTimeImmutable();
213 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
214 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
215 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
216 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
218 $closestMonday = closest($dt->format('d'), $mondays);
220 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays));
222 $tasksForTheWeek = getTasksForTheWeek(
223 array_search($closestMonday, $mondays),
224 (int)$dt->format('m'),
225 require 'taskMatrix.php'
228 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
230 if (!is_dir(dirname($completedTasksFile))) {
231 mkdir(dirname($completedTasksFile), 0777, true
);
234 if (in_array($arguments, $completedTasks)) {
235 $this->replyWithMessage(['text' => getString('taskAlreadyCompleted')]);
239 if (!in_array($arguments, $tasksForTheWeek)) {
240 $this->replyWithMessage(['text' => getString('unknownTask')]);
250 $this->replyWithMessage(['text' => getString('taskCompleted')]);
255 getTelegram()->commandsHandler(true
); //must come afterwards because lolzer