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!",
53 'reply_markup' => json_encode(['remove_keyboard' => true
])
57 getTelegram()->addCommand(
58 new class extends Command
{
59 protected $name = 'searchmovies';
60 protected $description = 'Search through the movie collection';
62 public function handle($arguments) {
63 $top5 = ∘
(unlines
, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort(∘
(∘
f(field('similarity')), ⬄
, field('similarity'))));
64 $movies = map(function($movie) use ($arguments) {
65 similar_text($arguments, substr($movie, 0, -7) ?
: '', $perc);
66 return ['title' => $movie, 'similarity' => $perc];
67 })(scandir('/mnt/media/Movies'));
69 $this->replyWithMessage(
71 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies)
78 getTelegram()->addCommand(
79 new class extends Command
{
80 protected $name = 'chatid';
81 protected $description = 'Get the id for this chat.';
83 public function handle($arguments) {
84 $this->replyWithMessage([
85 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
91 getTelegram()->addCommand(
92 new class extends Command
{
93 protected $name = 'mybills';
94 protected $description = 'List my bills';
96 public function handle($arguments) {
97 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
98 $this->replyWithChatAction(['action' => Actions
::TYPING
]);
99 $this->replyWithMessage([
105 "%s: $%s each due on the %s",
107 splitBill($bill['amount']),
108 $bill['due']->format('jS \of M')
111 getMessagesFromInbox(
113 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
118 ) ?
: ['You have no unpaid bills! Nice one.']
125 getTelegram()->addCommand(
126 new class extends Command
{
127 protected $name = 'paybill';
128 protected $description = 'Mark a bill as paid';
130 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 error_log(print_r($buttons, true
));
144 $this->replyWithMessage([
145 'text' => getMessageSenderDisplayName($this->getUpdate()). ' doesn\'t have any outstanding bills. Nice :)',
146 'reply_markup' => json_encode(['remove_keyboard' => true
])
151 $reply_markup = getTelegram()->replyKeyboardMarkup([
152 'keyboard' => $buttons,
153 'resize_keyboard' => true
,
154 'one_time_keyboard' => true
,
158 $this->replyWithMessage(
160 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which bill did you want to pay?',
161 'parse_mode' => 'markdown',
162 'reply_markup' => $reply_markup
170 getTelegram()->addCommand(
171 new class extends Command
{
172 protected $name = 'tasks';
173 protected $description = 'List tasks for this week';
175 public function handle($arguments) {
176 $dt = new DateTimeImmutable();
177 $directory = sprintf(
185 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
186 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
187 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
188 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
190 $currentMonth = (int)(new DateTimeImmutable())->format('m');
191 $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
192 $currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
193 $currentYear = (int)(new DateTimeImmutable())->format('Y');
194 $tasksForTheWeek = getTasksForTheWeek(
196 (int)$dt->format('m'),
197 require 'taskMatrix.php'
200 $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
201 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
203 $this->replyWithMessage([
204 'text' => ununlines([
205 getString('tasksForTheWeek'),
206 unlines(map(getStringAndCode
)(array_diff($tasksForTheWeek, $completedTasks)))
213 getTelegram()->addCommand(
214 new class extends Command
{
215 protected $name = 'completetask';
216 protected $description = 'Mark a task as completed';
218 public function handle($arguments) {
219 $dt = new DateTimeImmutable();
221 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
222 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
223 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
224 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
226 $closestMonday = closest($dt->format('d'), $mondays);
228 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays));
230 $tasksForTheWeek = getTasksForTheWeek(
231 array_search($closestMonday, $mondays),
232 (int)$dt->format('m'),
233 require 'taskMatrix.php'
236 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
238 if (!is_dir(dirname($completedTasksFile))) {
239 mkdir(dirname($completedTasksFile), 0777, true
);
242 if (in_array($arguments, $completedTasks)) {
243 $this->replyWithMessage(['text' => getString('taskAlreadyCompleted')]);
247 if (!in_array($arguments, $tasksForTheWeek)) {
248 $this->replyWithMessage(['text' => getString('unknownTask')]);
258 $this->replyWithMessage(['text' => getString('taskCompleted')]);
263 getTelegram()->commandsHandler(true
); //must come afterwards because lolzer