1 <?php
declare(strict_types
=1);
5 require 'vendor/autoload.php';
6 require 'Commands/MyBills.php';
7 require '../../config.php';
10 use DateTimeImmutable
;
12 function getTelegram() {
14 return $tg = $tg ??
new \Telegram\Bot\
Api(BOT_TOKEN
);
17 function splitBill($amount) {
18 return floor($amount/2);
21 function identity($x) {
25 function getMessageSender($update) {
26 return PARTICIPANT_IDS
[$update->get('message')->get('from')->get('id')];
29 function getMessageSenderDisplayName($update) {
30 return $update->get('message')->get('from')->get('first_name');
33 function canChatWith($update) {
34 return in_array($update->get('message')->get('from')->get('id'), array_keys(PARTICIPANT_IDS
));
37 function debug($whatever) {
43 function getInbox($inbox) {
46 if (!isset($inboxes[$inbox])) {
47 $inboxes[$inbox] = imap_open(
48 '{imap.gmail.com:993/debug/imap/ssl/novalidate-cert}' . $inbox,
54 return $inboxes[$inbox];
57 function getMessagesFromInbox($inbox, array $rules, $unseenOnly = true
) {
60 function($rule, $service) use ($inbox, $unseenOnly) {
61 $emails = imap_search($inbox, ['SEEN ', 'UNSEEN '][$unseenOnly] . $rule['imapQuery'], SE_UID
);
67 $body = imap_fetchbody($inbox, $emails[0], '1', FT_UID
);
68 preg_match($rule['regex'], $rule['messageTransform']($body), $matches);
71 'service' => $service,
72 'id' => substr(md5($body), 0, 6),
74 'telegramMessage' => $rule['telegramMessage'],
75 'due' => new DateTimeImmutable($rule['dateTransform']($matches['due'])),
76 'amount' => $matches['amount']