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) {
130 $buttons = [array_values(map(function($bill) {
131 return sprintf('%s%s', hide('[billid]' . $bill['id'] . '[billid]'), $bill['service'] . ' ($' . splitBill($bill['amount']) . ')');
132 })(getMessagesFromInbox(
134 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
140 $reply_markup = getTelegram()->replyKeyboardMarkup([
141 'keyboard' => $buttons,
142 'resize_keyboard' => true
,
143 'one_time_keyboard' => true
,
144 'remove_keyboard' => true
,
148 $this->replyWithMessage(
150 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSender($this->getUpdate()) . '), which bill did you want to pay?',
151 'parse_mode' => 'markdown',
152 'reply_markup' => $reply_markup
160 getTelegram()->addCommand(
161 new class extends Command
{
162 protected $name = 'tasks';
163 protected $description = 'List tasks for this week';
165 public function handle($arguments) {
166 $dt = new DateTimeImmutable();
167 $directory = sprintf(
175 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
176 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
177 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
178 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
180 $currentMonth = (int)(new DateTimeImmutable())->format('m');
181 $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
182 $currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
183 $currentYear = (int)(new DateTimeImmutable())->format('Y');
184 $tasksForTheWeek = getTasksForTheWeek(
186 (int)$dt->format('m'),
187 require 'taskMatrix.php'
190 $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
191 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
193 $this->replyWithMessage([
194 'text' => ununlines([
195 getString('tasksForTheWeek'),
196 unlines(map(getStringAndCode
)(array_diff($tasksForTheWeek, $completedTasks)))
203 getTelegram()->addCommand(
204 new class extends Command
{
205 protected $name = 'completetask';
206 protected $description = 'Mark a task as completed';
208 public function handle($arguments) {
209 $dt = new DateTimeImmutable();
211 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
212 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
213 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
214 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
216 $closestMonday = closest($dt->format('d'), $mondays);
218 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays));
220 $tasksForTheWeek = getTasksForTheWeek(
221 array_search($closestMonday, $mondays),
222 (int)$dt->format('m'),
223 require 'taskMatrix.php'
226 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
228 if (!is_dir(dirname($completedTasksFile))) {
229 mkdir(dirname($completedTasksFile), 0777, true
);
232 if (in_array($arguments, $completedTasks)) {
233 $this->replyWithMessage(['text' => getString('taskAlreadyCompleted')]);
237 if (!in_array($arguments, $tasksForTheWeek)) {
238 $this->replyWithMessage(['text' => getString('unknownTask')]);
248 $this->replyWithMessage(['text' => getString('taskCompleted')]);
253 getTelegram()->commandsHandler(true
); //must come afterwards because lolzer