Update date format to include the month
[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 getTelegram()->addCommand(
18 new class extends Command {
19 protected $name = 'chatid';
20 protected $description = 'Get the id for this chat.';
21
22 public function handle($arguments) {
23 $this->replyWithMessage([
24 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
25 ]);
26 }
27 }
28 );
29
30 getTelegram()->addCommand(
31 new class extends Command {
32 protected $name = 'mybills';
33 protected $description = 'List my bills';
34
35 public function handle($arguments) {
36 if (!canChatWith($this->getUpdate())) {
37 $this->replyWithMessage(['text' => "Sorry, Dad says I can't talk to you."]);
38 return;
39 }
40
41 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
42 $this->replyWithChatAction(['action' => Actions::TYPING]);
43 $this->replyWithMessage([
44 'text' => implode(
45 "\n",
46 array_map(
47 function($bill) {
48 return sprintf(
49 "%s (%s): $%s each due on the %s",
50 $bill['service'],
51 $bill['id'],
52 splitBill($bill['amount']),
53 $bill['due']->format('jS \of M')
54 );
55 },
56 getMessagesFromInbox(
57 getInbox(
58 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
59 ),
60 require 'rules.php',
61 FALSE
62 )
63 )
64 )
65 ]);
66 }
67 }
68 );
69
70 getTelegram()->addCommand(
71 new class extends Command {
72 protected $name = 'paybill';
73 protected $description = 'Mark a bill as paid';
74
75 public function handle($arguments) {
76 if (!canChatWith($this->getUpdate())) {
77 $this->replyWithMessage(['text' => "Sorry, Dad says I can't talk to you."]);
78 return;
79 }
80
81 if (!$arguments) {
82 $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..."]);
83 $this->triggerCommand('mybills');
84 }
85
86 $messages = array_values(array_filter(
87 getMessagesFromInbox(
88 getInbox(
89 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
90 ),
91 require 'rules.php',
92 FALSE
93 ),
94 function($e) use ($arguments) {
95 return $e['id'] == $arguments;
96 }
97 ));
98
99 if(!$messages || count($messages) !== 1) {
100 $this->replyWithMessage(['text' => "That doesn't look like a valid id. Use /mybills to list your bills."]);
101 }
102
103 imap_delete(getInbox('Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'), $messages[0]['uid'], FT_UID);
104 $this->replyWithMessage(['text' => "I marked " . getMessageSenderDisplayName($this->getUpdate()) . " as having paid the " . strtolower($messages[0]['service']) . " bill, thanks!"]);
105 }
106 }
107
108 );
109
110 getTelegram()->commandsHandler(true); //must come afterwards because lolzer