e6aaf8f57daa15d52fda855b0e583fa5485ac5b7
[SonOfLokstallBot.git] / Commands / MyBills.php
1 <?php declare(strict_types=1);
2
3 namespace Commands;
4
5 use Telegram\Bot\Actions;
6 use Telegram\Bot\Commands\Command;
7
8 class MyBills extends Command {
9 CONST CAM = 131202815;
10 CONST ASH = 242626501;
11
12 protected $name = "mybills";
13
14 protected $description = "List my bills 💰";
15
16 private static function splitBill($amount) {
17 return floor($amount/2);
18 }
19
20 private static function identity($x) {
21 return $x;
22 }
23
24 private function generateMessages($imapResource, $rules) {
25 return array_filter(array_map(
26 function($email) use ($imapResource, $reminder) {
27 $emails = imap_search($imapResource, 'SEEN ' . $email['imapQuery']);
28
29 if(!$emails) {
30 return '';
31 }
32
33 preg_match($email['regex'], $email['messageTransform'](imap_fetchbody($imapResource, $emails[0], 1)), $matches);
34
35 return sprintf(
36 $email['telegramMessage'],
37 splitBill($matches['amount']),
38 (new DateTimeImmutable(
39 $email['dateTransform']($matches['due'])
40 ))->format('jS')
41 );
42 },
43 $rules
44 ), function($message) {
45 return !!$message;
46 });
47 }
48
49 public function handle($arguments)
50 {
51
52 if ((int)$this->getUpdate()->get('message')->get('from')->get('id') == self::CAM) {
53 $inbox = 'Utilities/Cam To Pay';
54 } elseif ((int)$this->getUpdate()->get('message')->get('from')->get('id') == self::ASH) {
55 $inbox = 'Utilities/Ash To Pay';
56 }
57
58 $mailbox = '{imap.gmail.com:993/debug/imap/ssl/novalidate-cert}' . $inbox;
59 $username = 'molelord@gmail.com';
60 $password = '#NiceMeme420!';
61
62 $memes = [
63 'Electricity' => [
64 'imapQuery' => 'FROM "@synergy.net.au"',
65 'regex' => '/New charges: \$(?<amount>[0-9]+(\.[0-9]{2})?) Due (?<due>\d{1,2} \w{3} \d+)/',
66 'messageTransform' => 'identity',
67 'dateTransform' => 'identity',
68 'telegramMessage' => "Electricity bill: $%s each due on the %s"
69 ],
70 'Water' => [
71 'imapQuery' => 'FROM "@watercorporation.com.au"',
72 'regex' => '/Due date:.*(?<due>\d{1,2}\/\d{2}\/\d{4}).*Amount to pay:.*\$(?<amount>[0-9]+(\.[0-9]{2})?)/',
73 'messageTransform' => function($message) {
74 return implode(" ", array_map('trim', explode("\n", strip_tags(base64_decode($message)))));
75 },
76 'dateTransform' => function($date) {
77 return str_replace('/', '-', $date);
78 },
79 'telegramMessage' => "Water bill: $%s each due on the %s"
80 ],
81 'Internet' => [
82 'imapQuery' => 'FROM "@online.telstra.com"',
83 'regex' => '/Total \$(?<amount>[0-9]+(\.[0-9]{2})?).*Due Date (?<due>\d{1,2} \w{3} \d{4})/',
84 'messageTransform' => function($message) {
85 return implode(" ", array_map('trim', explode("\n", strip_tags(html_entity_decode($message)))));
86 },
87 'dateTransform' => 'identity',
88 'telegramMessage' => "Internet bill: $%s each due on the %s"
89 ],
90 'Gas' => [
91 'imapQuery' => 'FROM "@energy.agl.com.au"',
92 'regex' => '/Direct Debit amount: \$(?<amount>[0-9]+(\.[0-9]{2})?).*Direct Debit date: (?<due>\d{1,2} \w{3} \d+)/',
93 'messageTransform' => function($message) {
94 return implode(" ", array_map('trim', explode("\n", $message)));
95 },
96 'dateTransform' => 'identity',
97 'telegramMessage' => "Gas bill: $%s each due on the %s"
98 ]
99 ];
100
101 $imapResource = imap_open($mailbox, $username, $password);
102 $messages = $this->generateMessages($imapResource, $memes);
103 $this->replyWithMessage(['text' => $this->getUpdate()->get('message')->get('from')->get('id')]);
104 }
105 }