1678cb205371651674ded237a56c1aa9a8337345
[SonOfLokstallBot.git] / purjolok.php
1 <?php declare(strict_types=1);
2
3 require 'common.php';
4
5 use function Common\{
6 getTelegram,
7 getMessagesFromInbox,
8 getInbox,
9 splitBill,
10 getMessageSender,
11 getMessageSenderDisplayName,
12 canChatWith
13 };
14 use Telegram\Bot\Actions;
15 use Telegram\Bot\Commands\Command;
16
17
18 getTelegram()->addCommand(
19 new class extends Command {
20 protected $name = 'searchmovies';
21 protected $description = 'Search through the movie collection';
22
23 public function handle($arguments) {
24 $results = array_map(
25 function($moviename) use ($arguments) {
26 similar_text($arguments, substr($moviename, 0, -7) ?: '', $perc);
27 return ['title' => $moviename, 'similarity' => $perc];
28 },
29 scandir('/mnt/media/Movies')
30 );
31
32 uasort($results, function($a, $b) {
33 return $b['similarity'] <=> $a['similarity'];
34 });
35
36 $results = array_column(array_slice(array_values($results), 0, 5), 'title');
37 $this->replyWithMessage(['text' => "Here are the most similar movies titles I could find...\n\n" . implode("\n", $results)]);
38 }
39 }
40 );
41
42 getTelegram()->addCommand(
43 new class extends Command {
44 protected $name = 'chatid';
45 protected $description = 'Get the id for this chat.';
46
47 public function handle($arguments) {
48 $this->replyWithMessage([
49 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
50 ]);
51 }
52 }
53 );
54
55 getTelegram()->addCommand(
56 new class extends Command {
57 protected $name = 'mybills';
58 protected $description = 'List my bills';
59
60 public function handle($arguments) {
61 if (!canChatWith($this->getUpdate())) {
62 $this->replyWithMessage(['text' => "Sorry, Dad says I can't talk to you."]);
63 return;
64 }
65
66 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
67 $this->replyWithChatAction(['action' => Actions::TYPING]);
68 $this->replyWithMessage([
69 'text' => implode(
70 "\n",
71 array_map(
72 function($bill) {
73 return sprintf(
74 "%s (%s): $%s each due on the %s",
75 $bill['service'],
76 $bill['id'],
77 splitBill($bill['amount']),
78 $bill['due']->format('jS \of M')
79 );
80 },
81 getMessagesFromInbox(
82 getInbox(
83 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
84 ),
85 require 'rules.php',
86 FALSE
87 )
88 )
89 )
90 ]);
91 }
92 }
93 );
94
95 getTelegram()->addCommand(
96 new class extends Command {
97 protected $name = 'paybill';
98 protected $description = 'Mark a bill as paid';
99
100 public function handle($arguments) {
101 if (!canChatWith($this->getUpdate())) {
102 $this->replyWithMessage(['text' => "Sorry, Dad says I can't talk to you."]);
103 return;
104 }
105
106 if (!$arguments) {
107 $this->replyWithMessage(['text' => "I need the bill id. The /mybills command lists all your bills with the ids in brackets. Here, I'll show you..."]);
108 $this->triggerCommand('mybills');
109 }
110
111 $messages = array_values(array_filter(
112 getMessagesFromInbox(
113 getInbox(
114 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
115 ),
116 require 'rules.php',
117 FALSE
118 ),
119 function($e) use ($arguments) {
120 return $e['id'] == $arguments;
121 }
122 ));
123
124 if(!$messages || count($messages) !== 1) {
125 $this->replyWithMessage(['text' => "That doesn't look like a valid id. Use /mybills to list your bills."]);
126 }
127
128 imap_delete(getInbox('Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'), $messages[0]['uid'], FT_UID);
129 $this->replyWithMessage(['text' => "I marked " . getMessageSenderDisplayName($this->getUpdate()) . " as having paid the " . strtolower($messages[0]['service']) . " bill, thanks!"]);
130 }
131 }
132
133 );
134
135 getTelegram()->commandsHandler(true); //must come afterwards because lolzer