Add a message for when there are no bills
[SonOfLokstallBot.git] / common.php
1 <?php declare(strict_types=1);
2
3 require_once('vendor/autoload.php');
4 require_once(__DIR__ . '/config.php');
5
6 use Telegram\Bot\Api;
7
8 function getTelegram() {
9 STATIC $tg;
10 return $tg = $tg ?? new \Telegram\Bot\Api(BOT_TOKEN);
11 }
12
13 function splitBill($amount) {
14 return floor($amount/2);
15 }
16
17 function identity($x) {
18 return $x;
19 }
20
21 function getMessageSender($update) {
22 return PARTICIPANT_IDS[$update->get('message')->get('from')->get('id')];
23 }
24
25 function getMessageSenderDisplayName($update) {
26 return $update->get('message')->get('from')->get('first_name');
27 }
28
29 function canChatWith($update) {
30 return in_array($update->get('message')->get('from')->get('id'), array_keys(PARTICIPANT_IDS));
31 }
32
33 function debug($whatever) {
34 echo '<pre>';
35 print_r($whatever);
36 echo '</pre>';
37 }
38
39 function getInbox($inbox) {
40 STATIC $inboxes;
41
42 if (!isset($inboxes[$inbox])) {
43 $inboxes[$inbox] = imap_open(
44 '{imap.gmail.com:993/debug/imap/ssl/novalidate-cert}' . $inbox,
45 EMAIL,
46 PASSWORD
47 );
48 }
49
50 return $inboxes[$inbox];
51 }
52
53 function getRules() {
54 STATIC $rules;
55 return $rules = $rules ?? require 'rules.php';
56 }
57
58 function getString($identifier, ...$vars) {
59 STATIC $strings;
60 $strings = $strings ?? require 'strings.php';
61
62 return sprintf($strings[$identifier], ...$vars);
63 }
64
65 function formatDate($date) {
66 return $date->format(DATE_FORMAT);
67 }
68
69 function ssort($comparitor) {
70 return function($array) use ($comparitor) {
71 uasort($array, uncurry($comparitor));
72 return $array;
73 };
74 }
75
76 function uncurry($f) {
77 return function($a, $b) use ($f) {
78 return $f($a)($b);
79 };
80 }
81
82 const sendToGroupChat = 'sendToGroupChat';
83 function sendToGroupChat(string $message) {
84 return getTelegram()->sendMessage(['chat_id' => CHAT_ID, 'text' => $message]);
85 }
86
87 const generateReminderText = 'generateReminderText';
88 function generateReminderText($message) {
89 return getString('billreminder', REMIND_THRESHOLD, $message['service'], splitBill($message['amount']), formatDate($message['due']));
90 }
91
92 const generateNewBillText = 'generateNewBillText';
93 function generateNewBillText($message) {
94 return getString('newbill', $message['service'], splitBill($message['amount']), formatDate($message['due']));
95 }
96
97 const messageNeedsReminder = 'messageNeedsReminder';
98 function messageNeedsReminder($message) {
99 return $message['due']->diff(new DateTimeImmutable)->d == REMIND_THRESHOLD;
100 }
101
102 const unlines = 'unlines';
103 function unlines($lines) {
104 return implode("\n", $lines);
105 }
106
107 function field($field) {
108 return function($array) use ($field) {
109 return $array[$field];
110 };
111 }
112
113 const= '⬄';
114 function($a) {
115 return function($b) use ($a) {
116 return $a <=> $b;
117 };
118 }
119
120
121 function(...$fs) {
122 return function($arg) use ($fs) {
123 return array_reduce(array_reverse($fs), function($c, $f) {
124 return $f($c);
125 }, $arg);
126 };
127 }
128
129 function map($callable) {
130 return function($list) use ($callable) {
131 return array_map($callable, $list);
132 };
133 }
134
135 function aaray_column($column) {
136 return function($array) use ($column) {
137 return array_column($array, $column);
138 };
139 }
140
141 function aaray_slice($start) {
142 return function($length) use ($start) {
143 return function($array) use ($length, $start) {
144 return array_slice($array, $start, $length);
145 };
146 };
147 }
148
149 function filter($callable) {
150 return function($list) use ($callable) {
151 return array_filter($list, $callable);
152 };
153 }
154
155 function f∘(callable $f) {
156 return function(callable $g) use ($f) {
157 return function($arg) use($g, $f) {
158 return $f($g($arg));
159 };
160 };
161 }
162
163 functionf(callable $f) {
164 return function(callable $g) use ($f) {
165 return function($arg) use($g, $f) {
166 return $g($f($arg));
167 };
168 };
169 }
170
171 function($a, $b) {
172 return array_merge($a, $b);
173 }
174
175 function getMessagesFromInbox($inbox, array $rules, $unseenOnly = true) {
176 return array_filter(
177 array_map(
178 function($rule, $service) use ($inbox, $unseenOnly) {
179 $emails = imap_search($inbox, ['SEEN ', 'UNSEEN '][$unseenOnly] . $rule['imapQuery'], SE_UID);
180
181 if(!$emails) {
182 return [];
183 }
184
185 $messageTransform = $rule['messageTransform'] ?? 'identity';
186 $dateTransform = $rule['dateTransform'] ?? 'identity';
187
188 $body = imap_fetchbody($inbox, $emails[0], '1', FT_UID);
189 preg_match($rule['regex'], $messageTransform($body), $matches);
190
191 return [
192 'service' => $service,
193 'id' => substr(md5($body), 0, 6),
194 'uid' => $emails[0],
195 'due' => new DateTimeImmutable($dateTransform($matches['due'])),
196 'amount' => $matches['amount']
197 ];
198 },
199 $rules,
200 array_keys($rules)
201 ),
202 function($e) {
203 return !!$e;
204 }
205 );
206 }