1 <?php
declare(strict_types
=1);
5 require 'vendor/autoload.php';
6 require __DIR__
. '/../../config.php';
11 function getTelegram() {
13 return $tg = $tg ??
new \Telegram\Bot\
Api(BOT_TOKEN
);
16 function splitBill($amount) {
17 return floor($amount/2);
20 function identity($x) {
24 function getMessageSender($update) {
25 return PARTICIPANT_IDS
[$update->get('message')->get('from')->get('id')];
28 function getMessageSenderDisplayName($update) {
29 return $update->get('message')->get('from')->get('first_name');
32 function canChatWith($update) {
33 return in_array($update->get('message')->get('from')->get('id'), array_keys(PARTICIPANT_IDS
));
36 function debug($whatever) {
42 function getInbox($inbox) {
45 if (!isset($inboxes[$inbox])) {
46 $inboxes[$inbox] = imap_open(
47 '{imap.gmail.com:993/debug/imap/ssl/novalidate-cert}' . $inbox,
53 return $inboxes[$inbox];
56 function getMessagesFromInbox($inbox, array $rules, $unseenOnly = true
) {
59 function($rule, $service) use ($inbox, $unseenOnly) {
60 $emails = imap_search($inbox, ['SEEN ', 'UNSEEN '][$unseenOnly] . $rule['imapQuery'], SE_UID
);
66 $body = imap_fetchbody($inbox, $emails[0], '1', FT_UID
);
67 preg_match($rule['regex'], $rule['messageTransform']($body), $matches);
70 'service' => $service,
71 'id' => substr(md5($body), 0, 6),
73 'telegramMessage' => $rule['telegramMessage'],
74 'due' => new DateTimeImmutable($rule['dateTransform']($matches['due'])),
75 'amount' => $matches['amount']