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