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 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.",
53 'reply_markup' => json_encode(['remove_keyboard' => true
])
56 getTelegram()->sendSticker([
57 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
58 'sticker' => 'CAADBQADOgAD__7RBxFhadMBV3g5Ag'
62 getTelegram()->addCommand(
63 new class extends Command
{
64 protected $name = 'searchmovies';
65 protected $description = 'Search through the movie collection';
67 public function handle($arguments) {
68 $top5 = ∘
(unlines
, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort(∘
(∘
f(field('similarity')), ⬄
, field('similarity'))));
69 $movies = map(function($movie) use ($arguments) {
70 similar_text($arguments, substr($movie, 0, -7) ?
: '', $perc);
71 return ['title' => $movie, 'similarity' => $perc];
72 })(scandir('/mnt/media/Movies'));
74 $this->replyWithMessage(
76 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies)
83 getTelegram()->addCommand(
84 new class extends Command
{
85 protected $name = 'chatid';
86 protected $description = 'Get the id for this chat.';
88 public function handle($arguments) {
89 $this->replyWithMessage([
90 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
96 getTelegram()->addCommand(
97 new class extends Command
{
98 protected $name = 'mybills';
99 protected $description = 'List my bills';
101 public function handle($arguments) {
102 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
103 $this->replyWithChatAction(['action' => Actions
::TYPING
]);
104 $this->replyWithMessage([
110 "%s: $%s each due on the %s",
112 splitBill($bill['amount']),
113 $bill['due']->format('jS \of M')
116 getMessagesFromInbox(
118 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
123 ) ?
: ['You have no unpaid bills! Nice one.']
130 getTelegram()->addCommand(
131 new class extends Command
{
132 protected $name = 'paybill';
133 protected $description = 'Mark a bill as paid';
135 public function handle($arguments) {
136 $buttons = [array_values(map(function($bill) {
137 return sprintf('%s%s', hide('[billid]' . $bill['id'] . '[billid]'), $bill['service'] . ' ($' . splitBill($bill['amount']) . ')');
138 })(getMessagesFromInbox(
140 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
146 error_log(print_r($buttons, true
));
149 $this->replyWithMessage([
150 'text' => getMessageSenderDisplayName($this->getUpdate()). ' doesn\'t have any outstanding bills. Nice :)',
151 'reply_markup' => json_encode(['remove_keyboard' => true
])
156 $reply_markup = getTelegram()->replyKeyboardMarkup([
157 'keyboard' => $buttons,
158 'resize_keyboard' => true
,
159 'one_time_keyboard' => true
,
163 $this->replyWithMessage(
165 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which bill did you want to pay?',
166 'parse_mode' => 'markdown',
167 'reply_markup' => $reply_markup
175 getTelegram()->addCommand(
176 new class extends Command
{
177 protected $name = 'tasks';
178 protected $description = 'List tasks for this week';
180 public function handle($arguments) {
181 $dt = new DateTimeImmutable();
182 $directory = sprintf(
190 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
191 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
192 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
193 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
195 $currentMonth = (int)(new DateTimeImmutable())->format('m');
196 $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
197 $currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
198 $currentYear = (int)(new DateTimeImmutable())->format('Y');
199 $tasksForTheWeek = getTasksForTheWeek(
201 (int)$dt->format('m'),
202 require 'taskMatrix.php'
205 $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
206 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
208 $this->replyWithMessage([
209 'text' => ununlines([
210 getString('tasksForTheWeek'),
211 unlines(map(getStringAndCode
)(array_diff($tasksForTheWeek, $completedTasks)))
218 getTelegram()->addCommand(
219 new class extends Command
{
220 protected $name = 'completetask';
221 protected $description = 'Mark a task as completed';
223 public function handle($arguments) {
224 $dt = new DateTimeImmutable();
226 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
227 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
228 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
229 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
231 $closestMonday = closest($dt->format('d'), $mondays);
233 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays));
235 $tasksForTheWeek = getTasksForTheWeek(
236 array_search($closestMonday, $mondays),
237 (int)$dt->format('m'),
238 require 'taskMatrix.php'
241 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
243 if (!is_dir(dirname($completedTasksFile))) {
244 mkdir(dirname($completedTasksFile), 0777, true
);
247 if (in_array($arguments, $completedTasks)) {
248 $this->replyWithMessage(['text' => getString('taskAlreadyCompleted')]);
252 if (!in_array($arguments, $tasksForTheWeek)) {
253 $this->replyWithMessage(['text' => getString('unknownTask')]);
263 $this->replyWithMessage(['text' => getString('taskCompleted')]);
268 getTelegram()->commandsHandler(true
); //must come afterwards because lolzer