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