Initial commit
authorCameron Ball <cameron@cameron1729.xyz>
Fri, 16 Nov 2018 03:23:23 +0000 (11:23 +0800)
committerCameron Ball <cameron@cameron1729.xyz>
Fri, 16 Nov 2018 03:23:23 +0000 (11:23 +0800)
.gitignore [new file with mode: 0644]
Commands/MyBills.php [new file with mode: 0644]
bins.php [new file with mode: 0644]
common.php [new file with mode: 0644]
composer.json [new file with mode: 0644]
composer.lock [new file with mode: 0644]
purjolok.php [new file with mode: 0644]
readEmailsAndSendMessages.php [new file with mode: 0644]
rules.php [new file with mode: 0644]
wednesday.php [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..4f4773f
--- /dev/null
@@ -0,0 +1 @@
+config.php
diff --git a/Commands/MyBills.php b/Commands/MyBills.php
new file mode 100644 (file)
index 0000000..e6aaf8f
--- /dev/null
@@ -0,0 +1,105 @@
+<?php declare(strict_types=1);
+
+namespace Commands;
+
+use Telegram\Bot\Actions;
+use Telegram\Bot\Commands\Command;
+
+class MyBills extends Command {
+    CONST CAM = 131202815;
+    CONST ASH = 242626501;
+
+    protected $name = "mybills";
+
+    protected $description = "List my bills 💰";
+
+    private static function splitBill($amount) {
+        return floor($amount/2);
+    }
+
+    private static function identity($x) {
+        return $x;
+    }
+
+    private function generateMessages($imapResource, $rules) {
+        return array_filter(array_map(
+            function($email) use ($imapResource, $reminder) {
+                $emails = imap_search($imapResource, 'SEEN ' . $email['imapQuery']);
+
+                if(!$emails) {
+                    return '';
+                }
+
+                preg_match($email['regex'], $email['messageTransform'](imap_fetchbody($imapResource, $emails[0], 1)), $matches);
+
+                return sprintf(
+                    $email['telegramMessage'],
+                    splitBill($matches['amount']),
+                    (new DateTimeImmutable(
+                        $email['dateTransform']($matches['due'])
+                    ))->format('jS')
+                );
+            },
+            $rules
+        ), function($message) {
+            return !!$message;
+        });
+    }
+
+    public function handle($arguments)
+    {
+
+        if ((int)$this->getUpdate()->get('message')->get('from')->get('id') == self::CAM) {
+            $inbox = 'Utilities/Cam To Pay';
+        } elseif ((int)$this->getUpdate()->get('message')->get('from')->get('id') == self::ASH) {
+            $inbox = 'Utilities/Ash To Pay';
+        }
+
+        $mailbox = '{imap.gmail.com:993/debug/imap/ssl/novalidate-cert}' . $inbox;
+        $username = 'molelord@gmail.com';
+        $password = '#NiceMeme420!';
+
+        $memes = [
+            'Electricity' => [
+                'imapQuery' => 'FROM "@synergy.net.au"',
+                'regex' => '/New charges: \$(?<amount>[0-9]+(\.[0-9]{2})?)  Due (?<due>\d{1,2} \w{3} \d+)/',
+                'messageTransform' => 'identity',
+                'dateTransform' => 'identity',
+                'telegramMessage' => "Electricity bill: $%s each due on the %s"
+            ],
+            'Water' => [
+                'imapQuery' => 'FROM "@watercorporation.com.au"',
+                'regex' => '/Due date:.*(?<due>\d{1,2}\/\d{2}\/\d{4}).*Amount to pay:.*\$(?<amount>[0-9]+(\.[0-9]{2})?)/',
+                'messageTransform' => function($message) {
+                    return implode(" ", array_map('trim', explode("\n", strip_tags(base64_decode($message)))));
+                },
+                'dateTransform' => function($date) {
+                    return str_replace('/', '-', $date);
+                },
+                'telegramMessage' => "Water bill: $%s each due on the %s"
+            ],
+            'Internet' => [
+                'imapQuery' => 'FROM "@online.telstra.com"',
+                'regex' => '/Total \$(?<amount>[0-9]+(\.[0-9]{2})?).*Due Date (?<due>\d{1,2} \w{3} \d{4})/',
+                'messageTransform' => function($message) {
+                    return implode(" ", array_map('trim', explode("\n", strip_tags(html_entity_decode($message)))));
+                },
+                'dateTransform' => 'identity',
+                'telegramMessage' => "Internet bill: $%s each due on the %s"
+            ],
+            'Gas' => [
+                'imapQuery' => 'FROM "@energy.agl.com.au"',
+                'regex' => '/Direct Debit amount: \$(?<amount>[0-9]+(\.[0-9]{2})?).*Direct Debit date: (?<due>\d{1,2} \w{3} \d+)/',
+                'messageTransform' => function($message) {
+                    return implode(" ", array_map('trim', explode("\n", $message)));
+                },
+                'dateTransform' => 'identity',
+                'telegramMessage' => "Gas bill: $%s each due on the %s"
+            ]
+        ];
+
+        $imapResource = imap_open($mailbox, $username, $password);
+        $messages = $this->generateMessages($imapResource, $memes);
+        $this->replyWithMessage(['text' => $this->getUpdate()->get('message')->get('from')->get('id')]);
+    }
+}
\ No newline at end of file
diff --git a/bins.php b/bins.php
new file mode 100644 (file)
index 0000000..8473ea4
--- /dev/null
+++ b/bins.php
@@ -0,0 +1,12 @@
+<?php declare(strict_types=1);
+
+require 'common.php';
+
+use function Common\getTelegram;
+
+getTelegram()->sendMessage(
+    [
+        'chat_id' => CHAT_ID,
+        'text' => 'Take out that BINZ #TrashSLAM 🚮'
+    ]
+);
diff --git a/common.php b/common.php
new file mode 100644 (file)
index 0000000..324fe6c
--- /dev/null
@@ -0,0 +1,86 @@
+<?php declare(strict_types=1);
+
+namespace Common;
+
+require 'vendor/autoload.php';
+require 'Commands/MyBills.php';
+require 'config.php';
+
+use Telegram\Bot\Api;
+use DateTimeImmutable;
+
+function getTelegram() {
+    STATIC $tg;
+    return $tg = $tg ?? new \Telegram\Bot\Api(BOT_TOKEN);
+}
+
+function splitBill($amount) {
+    return floor($amount/2);
+}
+
+function identity($x) {
+    return $x;
+}
+
+function getMessageSender($update) {
+    return PARTICIPANT_IDS[$update->get('message')->get('from')->get('id')];
+}
+
+function getMessageSenderDisplayName($update) {
+    return $update->get('message')->get('from')->get('first_name');
+}
+
+function canChatWith($update) {
+    return in_array($update->get('message')->get('from')->get('id'), array_keys(PARTICIPANT_IDS));
+}
+
+function debug($whatever) {
+    echo '<pre>';
+    print_r($whatever);
+    echo '</pre>';
+}
+
+function getInbox($inbox) {
+    STATIC $inboxes;
+
+    if (!isset($inboxes[$inbox])) {
+        $inboxes[$inbox] = imap_open(
+            '{imap.gmail.com:993/debug/imap/ssl/novalidate-cert}' . $inbox,
+            EMAIL,
+            PASSWORD
+        );
+    }
+
+    return $inboxes[$inbox];
+}
+
+function getMessagesFromInbox($inbox, array $rules, $unseenOnly = true) {
+    return array_filter(
+        array_map(
+            function($rule, $service) use ($inbox, $unseenOnly) {
+                $emails = imap_search($inbox, ['SEEN ', 'UNSEEN '][$unseenOnly] . $rule['imapQuery'], SE_UID);
+
+                if(!$emails) {
+                    return [];
+                }
+
+                $body = imap_fetchbody($inbox, $emails[0], '1', FT_UID);
+                preg_match($rule['regex'], $rule['messageTransform']($body), $matches);
+
+                return [
+                    'service' => $service,
+                    'id' => substr(md5($body), 0, 6),
+                    'uid' => $emails[0],
+                    'telegramMessage' => $rule['telegramMessage'],
+                    'due' => new DateTimeImmutable($rule['dateTransform']($matches['due'])),
+                    'amount' => $matches['amount']
+                ];
+            },
+            $rules,
+            array_keys($rules)
+        ),
+        function($e) {
+            return !!$e;
+        }
+    );
+}
diff --git a/composer.json b/composer.json
new file mode 100644 (file)
index 0000000..cda6a21
--- /dev/null
@@ -0,0 +1,5 @@
+{
+    "require": {
+        "irazasyed/telegram-bot-sdk": "^2.0"
+    }
+}
diff --git a/composer.lock b/composer.lock
new file mode 100644 (file)
index 0000000..fdd80a0
--- /dev/null
@@ -0,0 +1,753 @@
+{
+    "_readme": [
+        "This file locks the dependencies of your project to a known state",
+        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
+        "This file is @generated automatically"
+    ],
+    "content-hash": "4fc59fc846328b44db644ed8cd44face",
+    "packages": [
+        {
+            "name": "doctrine/inflector",
+            "version": "v1.2.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/doctrine/inflector.git",
+                "reference": "e11d84c6e018beedd929cff5220969a3c6d1d462"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/doctrine/inflector/zipball/e11d84c6e018beedd929cff5220969a3c6d1d462",
+                "reference": "e11d84c6e018beedd929cff5220969a3c6d1d462",
+                "shasum": ""
+            },
+            "require": {
+                "php": "^7.0"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "^6.2"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.2.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Doctrine\\Common\\Inflector\\": "lib/Doctrine/Common/Inflector"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Roman Borschel",
+                    "email": "roman@code-factory.org"
+                },
+                {
+                    "name": "Benjamin Eberlei",
+                    "email": "kontakt@beberlei.de"
+                },
+                {
+                    "name": "Guilherme Blanco",
+                    "email": "guilhermeblanco@gmail.com"
+                },
+                {
+                    "name": "Jonathan Wage",
+                    "email": "jonwage@gmail.com"
+                },
+                {
+                    "name": "Johannes Schmitt",
+                    "email": "schmittjoh@gmail.com"
+                }
+            ],
+            "description": "Common String Manipulations with regard to casing and singular/plural rules.",
+            "homepage": "http://www.doctrine-project.org",
+            "keywords": [
+                "inflection",
+                "pluralize",
+                "singularize",
+                "string"
+            ],
+            "time": "2017-07-22T12:18:28+00:00"
+        },
+        {
+            "name": "guzzlehttp/guzzle",
+            "version": "6.3.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/guzzle/guzzle.git",
+                "reference": "f4db5a78a5ea468d4831de7f0bf9d9415e348699"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/guzzle/guzzle/zipball/f4db5a78a5ea468d4831de7f0bf9d9415e348699",
+                "reference": "f4db5a78a5ea468d4831de7f0bf9d9415e348699",
+                "shasum": ""
+            },
+            "require": {
+                "guzzlehttp/promises": "^1.0",
+                "guzzlehttp/psr7": "^1.4",
+                "php": ">=5.5"
+            },
+            "require-dev": {
+                "ext-curl": "*",
+                "phpunit/phpunit": "^4.0 || ^5.0",
+                "psr/log": "^1.0"
+            },
+            "suggest": {
+                "psr/log": "Required for using the Log middleware"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "6.2-dev"
+                }
+            },
+            "autoload": {
+                "files": [
+                    "src/functions_include.php"
+                ],
+                "psr-4": {
+                    "GuzzleHttp\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Michael Dowling",
+                    "email": "mtdowling@gmail.com",
+                    "homepage": "https://github.com/mtdowling"
+                }
+            ],
+            "description": "Guzzle is a PHP HTTP client library",
+            "homepage": "http://guzzlephp.org/",
+            "keywords": [
+                "client",
+                "curl",
+                "framework",
+                "http",
+                "http client",
+                "rest",
+                "web service"
+            ],
+            "time": "2017-06-22T18:50:49+00:00"
+        },
+        {
+            "name": "guzzlehttp/promises",
+            "version": "v1.3.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/guzzle/promises.git",
+                "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646",
+                "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.5.0"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "^4.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.4-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "GuzzleHttp\\Promise\\": "src/"
+                },
+                "files": [
+                    "src/functions_include.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Michael Dowling",
+                    "email": "mtdowling@gmail.com",
+                    "homepage": "https://github.com/mtdowling"
+                }
+            ],
+            "description": "Guzzle promises library",
+            "keywords": [
+                "promise"
+            ],
+            "time": "2016-12-20T10:07:11+00:00"
+        },
+        {
+            "name": "guzzlehttp/psr7",
+            "version": "1.4.2",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/guzzle/psr7.git",
+                "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c",
+                "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.4.0",
+                "psr/http-message": "~1.0"
+            },
+            "provide": {
+                "psr/http-message-implementation": "1.0"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "~4.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.4-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "GuzzleHttp\\Psr7\\": "src/"
+                },
+                "files": [
+                    "src/functions_include.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Michael Dowling",
+                    "email": "mtdowling@gmail.com",
+                    "homepage": "https://github.com/mtdowling"
+                },
+                {
+                    "name": "Tobias Schultze",
+                    "homepage": "https://github.com/Tobion"
+                }
+            ],
+            "description": "PSR-7 message implementation that also provides common utility methods",
+            "keywords": [
+                "http",
+                "message",
+                "request",
+                "response",
+                "stream",
+                "uri",
+                "url"
+            ],
+            "time": "2017-03-20T17:10:46+00:00"
+        },
+        {
+            "name": "illuminate/contracts",
+            "version": "v5.5.17",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/illuminate/contracts.git",
+                "reference": "d9e269284eba43bd2e9e8d1f1ba12362b00ec096"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/illuminate/contracts/zipball/d9e269284eba43bd2e9e8d1f1ba12362b00ec096",
+                "reference": "d9e269284eba43bd2e9e8d1f1ba12362b00ec096",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=7.0",
+                "psr/container": "~1.0",
+                "psr/simple-cache": "~1.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "5.5-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Illuminate\\Contracts\\": ""
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Taylor Otwell",
+                    "email": "taylor@laravel.com"
+                }
+            ],
+            "description": "The Illuminate Contracts package.",
+            "homepage": "https://laravel.com",
+            "time": "2017-09-19T13:09:37+00:00"
+        },
+        {
+            "name": "illuminate/support",
+            "version": "v5.5.17",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/illuminate/support.git",
+                "reference": "132b06edaab3808f63943004911d58785f164ab4"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/illuminate/support/zipball/132b06edaab3808f63943004911d58785f164ab4",
+                "reference": "132b06edaab3808f63943004911d58785f164ab4",
+                "shasum": ""
+            },
+            "require": {
+                "doctrine/inflector": "~1.1",
+                "ext-mbstring": "*",
+                "illuminate/contracts": "5.5.*",
+                "nesbot/carbon": "^1.20",
+                "php": ">=7.0"
+            },
+            "replace": {
+                "tightenco/collect": "self.version"
+            },
+            "suggest": {
+                "illuminate/filesystem": "Required to use the composer class (5.2.*).",
+                "symfony/process": "Required to use the composer class (~3.3).",
+                "symfony/var-dumper": "Required to use the dd function (~3.3)."
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "5.5-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Illuminate\\Support\\": ""
+                },
+                "files": [
+                    "helpers.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Taylor Otwell",
+                    "email": "taylor@laravel.com"
+                }
+            ],
+            "description": "The Illuminate Support package.",
+            "homepage": "https://laravel.com",
+            "time": "2017-10-17T12:18:29+00:00"
+        },
+        {
+            "name": "irazasyed/telegram-bot-sdk",
+            "version": "v2.2.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/irazasyed/telegram-bot-sdk.git",
+                "reference": "2824278370b231dfc9172104db8acc23fc5ade94"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/irazasyed/telegram-bot-sdk/zipball/2824278370b231dfc9172104db8acc23fc5ade94",
+                "reference": "2824278370b231dfc9172104db8acc23fc5ade94",
+                "shasum": ""
+            },
+            "require": {
+                "guzzlehttp/guzzle": "~6.0",
+                "illuminate/support": "~5.0",
+                "php": ">=5.5.0"
+            },
+            "require-dev": {
+                "phpspec/prophecy": "^1.5",
+                "phpunit/phpunit": "^4.8 || ^5.0"
+            },
+            "suggest": {
+                "illuminate/container": "Hold dependencies to be injected in commands constructors",
+                "irazasyed/larasupport": "Allows you to use any Laravel Package in Lumen by adding support!"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "2.0-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Telegram\\Bot\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "BSD-3-Clause"
+            ],
+            "authors": [
+                {
+                    "name": "Syed Irfaq R.",
+                    "email": "syed+gh@lukonet.com",
+                    "homepage": "https://github.com/irazasyed"
+                }
+            ],
+            "description": "The Unofficial Telegram Bot API PHP SDK",
+            "homepage": "https://github.com/irazasyed/telegram-bot-sdk",
+            "keywords": [
+                "laravel",
+                "laravel telegram",
+                "telegram",
+                "telegram bot",
+                "telegram bot api",
+                "telegram php",
+                "telegram sdk"
+            ],
+            "time": "2017-01-05T09:01:54+00:00"
+        },
+        {
+            "name": "nesbot/carbon",
+            "version": "1.22.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/briannesbitt/Carbon.git",
+                "reference": "7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc",
+                "reference": "7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.0",
+                "symfony/translation": "~2.6 || ~3.0"
+            },
+            "require-dev": {
+                "friendsofphp/php-cs-fixer": "~2",
+                "phpunit/phpunit": "~4.0 || ~5.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.23-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Carbon\\": "src/Carbon/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Brian Nesbitt",
+                    "email": "brian@nesbot.com",
+                    "homepage": "http://nesbot.com"
+                }
+            ],
+            "description": "A simple API extension for DateTime.",
+            "homepage": "http://carbon.nesbot.com",
+            "keywords": [
+                "date",
+                "datetime",
+                "time"
+            ],
+            "time": "2017-01-16T07:55:07+00:00"
+        },
+        {
+            "name": "psr/container",
+            "version": "1.0.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-fig/container.git",
+                "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f",
+                "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.0.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Psr\\Container\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "PHP-FIG",
+                    "homepage": "http://www.php-fig.org/"
+                }
+            ],
+            "description": "Common Container Interface (PHP FIG PSR-11)",
+            "homepage": "https://github.com/php-fig/container",
+            "keywords": [
+                "PSR-11",
+                "container",
+                "container-interface",
+                "container-interop",
+                "psr"
+            ],
+            "time": "2017-02-14T16:28:37+00:00"
+        },
+        {
+            "name": "psr/http-message",
+            "version": "1.0.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-fig/http-message.git",
+                "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
+                "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.0.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Psr\\Http\\Message\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "PHP-FIG",
+                    "homepage": "http://www.php-fig.org/"
+                }
+            ],
+            "description": "Common interface for HTTP messages",
+            "homepage": "https://github.com/php-fig/http-message",
+            "keywords": [
+                "http",
+                "http-message",
+                "psr",
+                "psr-7",
+                "request",
+                "response"
+            ],
+            "time": "2016-08-06T14:39:51+00:00"
+        },
+        {
+            "name": "psr/simple-cache",
+            "version": "1.0.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-fig/simple-cache.git",
+                "reference": "753fa598e8f3b9966c886fe13f370baa45ef0e24"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/753fa598e8f3b9966c886fe13f370baa45ef0e24",
+                "reference": "753fa598e8f3b9966c886fe13f370baa45ef0e24",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.0.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Psr\\SimpleCache\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "PHP-FIG",
+                    "homepage": "http://www.php-fig.org/"
+                }
+            ],
+            "description": "Common interfaces for simple caching",
+            "keywords": [
+                "cache",
+                "caching",
+                "psr",
+                "psr-16",
+                "simple-cache"
+            ],
+            "time": "2017-01-02T13:31:39+00:00"
+        },
+        {
+            "name": "symfony/polyfill-mbstring",
+            "version": "v1.6.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/symfony/polyfill-mbstring.git",
+                "reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296",
+                "reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.3"
+            },
+            "suggest": {
+                "ext-mbstring": "For best performance"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.6-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Symfony\\Polyfill\\Mbstring\\": ""
+                },
+                "files": [
+                    "bootstrap.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Nicolas Grekas",
+                    "email": "p@tchwork.com"
+                },
+                {
+                    "name": "Symfony Community",
+                    "homepage": "https://symfony.com/contributors"
+                }
+            ],
+            "description": "Symfony polyfill for the Mbstring extension",
+            "homepage": "https://symfony.com",
+            "keywords": [
+                "compatibility",
+                "mbstring",
+                "polyfill",
+                "portable",
+                "shim"
+            ],
+            "time": "2017-10-11T12:05:26+00:00"
+        },
+        {
+            "name": "symfony/translation",
+            "version": "v3.3.10",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/symfony/translation.git",
+                "reference": "409bf229cd552bf7e3faa8ab7e3980b07672073f"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/symfony/translation/zipball/409bf229cd552bf7e3faa8ab7e3980b07672073f",
+                "reference": "409bf229cd552bf7e3faa8ab7e3980b07672073f",
+                "shasum": ""
+            },
+            "require": {
+                "php": "^5.5.9|>=7.0.8",
+                "symfony/polyfill-mbstring": "~1.0"
+            },
+            "conflict": {
+                "symfony/config": "<2.8",
+                "symfony/yaml": "<3.3"
+            },
+            "require-dev": {
+                "psr/log": "~1.0",
+                "symfony/config": "~2.8|~3.0",
+                "symfony/intl": "^2.8.18|^3.2.5",
+                "symfony/yaml": "~3.3"
+            },
+            "suggest": {
+                "psr/log": "To use logging capability in translator",
+                "symfony/config": "",
+                "symfony/yaml": ""
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "3.3-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Symfony\\Component\\Translation\\": ""
+                },
+                "exclude-from-classmap": [
+                    "/Tests/"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Fabien Potencier",
+                    "email": "fabien@symfony.com"
+                },
+                {
+                    "name": "Symfony Community",
+                    "homepage": "https://symfony.com/contributors"
+                }
+            ],
+            "description": "Symfony Translation Component",
+            "homepage": "https://symfony.com",
+            "time": "2017-10-02T06:42:24+00:00"
+        }
+    ],
+    "packages-dev": [],
+    "aliases": [],
+    "minimum-stability": "stable",
+    "stability-flags": [],
+    "prefer-stable": false,
+    "prefer-lowest": false,
+    "platform": [],
+    "platform-dev": []
+}
diff --git a/purjolok.php b/purjolok.php
new file mode 100644 (file)
index 0000000..69e22d4
--- /dev/null
@@ -0,0 +1,97 @@
+<?php declare(strict_types=1);
+
+require 'common.php';
+
+use function Common\{
+        getTelegram,
+        getMessagesFromInbox,
+        getInbox,
+        splitBill,
+        getMessageSender,
+        getMessageSenderDisplayName,
+        canChatWith
+};
+use Telegram\Bot\Actions;
+use Telegram\Bot\Commands\Command;
+
+getTelegram()->addCommand(
+    new class extends Command {
+        protected $name = 'mybills';
+        protected $description = 'List my bills';
+
+        public function handle($arguments) {
+            if (!canChatWith($this->getUpdate())) {
+                $this->replyWithMessage(['text' => "Sorry, Dad says I can't talk to you."]);
+                return;
+            }
+
+            $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate())  . "'s unpaid bills. Just a sec ..."]);
+            $this->replyWithChatAction(['action' => Actions::TYPING]);
+            $this->replyWithMessage([
+                'text' => implode(
+                    "\n",
+                    array_map(
+                        function($bill) {
+                            return sprintf(
+                                "%s (%s): $%s each due on the %s",
+                                $bill['service'],
+                                $bill['id'],
+                                splitBill($bill['amount']),
+                                $bill['due']->format('jS')
+                            );
+                        },
+                        getMessagesFromInbox(
+                            getInbox(
+                                'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
+                            ),
+                            require 'rules.php',
+                            FALSE
+                        )
+                    )
+                )
+            ]);
+        }
+    }
+);
+
+getTelegram()->addCommand(
+    new class extends Command {
+        protected $name = 'paybill';
+        protected $description = 'Mark a bill as paid';
+
+        public function handle($arguments) {
+            if (!canChatWith($this->getUpdate())) {
+                $this->replyWithMessage(['text' => "Sorry, Dad says I can't talk to you."]);
+                return;
+            }
+
+            if (!$arguments) {
+                $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..."]);
+                $this->triggerCommand('mybills');
+            }
+
+            $messages = array_values(array_filter(
+                getMessagesFromInbox(
+                    getInbox(
+                        'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
+                    ),
+                    require 'rules.php',
+                    FALSE
+                ),
+                function($e) use ($arguments) {
+                    return $e['id'] == $arguments;
+                }
+            ));
+
+            if(!$messages || count($messages) !== 1) {
+                $this->replyWithMessage(['text' => "That doesn't look like a valid id. Use /mybills to list your bills."]);
+            }
+
+            imap_delete(getInbox('Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'), $messages[0]['uid'], FT_UID);
+            $this->replyWithMessage(['text' => "I marked " . getMessageSenderDisplayName($this->getUpdate())  . " as having paid the " . strtolower($messages[0]['service']) . " bill, thanks!"]);
+        }
+    }
+
+);
+
+getTelegram()->commandsHandler(true); //must come afterwards because lolzer
diff --git a/readEmailsAndSendMessages.php b/readEmailsAndSendMessages.php
new file mode 100644 (file)
index 0000000..b13741e
--- /dev/null
@@ -0,0 +1,44 @@
+<?php declare(strict_types=1);
+
+require 'common.php';
+
+use function Common\{getTelegram, getInbox, getMessagesFromInbox, splitBill};
+
+array_map(
+    function($message) {
+        return getTelegram()->sendMessage(
+            [
+                'chat_id' => CHAT_ID,
+                'text'=> $message
+            ]
+        );
+    },
+    array_merge(
+        array_map(
+            function($message) {
+                return sprintf(
+                    '[REMINDER: %d DAYS] ' . $message['telegramMessage'],
+                    REMIND_THRESHOLD,
+                    splitBill($message['amount']),
+                    $message['due']->format('jS')
+                );
+            },
+            array_filter(
+                getMessagesFromInbox(getInbox('Multiple Inboxes/Needs Action'), require 'rules.php', false),
+                function($message) {
+                    return $message['due']->diff(new DateTimeImmutable)->d == REMIND_THRESHOLD;
+                }
+            )
+        ),
+        array_map(
+            function($message) {
+                return sprintf(
+                    $message['telegramMessage'],
+                    splitBill($message['amount']),
+                    $message['due']->format('jS')
+                );
+            },
+            getMessagesFromInbox(getInbox('Multiple Inboxes/Needs Action'), require 'rules.php')
+        )
+    )
+);
diff --git a/rules.php b/rules.php
new file mode 100644 (file)
index 0000000..e31341c
--- /dev/null
+++ b/rules.php
@@ -0,0 +1,40 @@
+<?php declare(strict_types=1);
+
+return [
+    'Electricity' => [
+        'imapQuery' => 'FROM "@synergy.net.au"',
+        'regex' => '/New charges: \$(?<amount>[0-9]+(\.[0-9]{2})?)  Due (?<due>\d{1,2} \w{3} \d+)/',
+        'messageTransform' => 'Common\identity',
+        'dateTransform' => 'Common\identity',
+        'telegramMessage' => "Electricity bill: $%s each due on the %s"
+    ],
+    'Water' => [
+        'imapQuery' => 'FROM "@watercorporation.com.au"',
+        'regex' => '/Due date:.*?(?<due>\d{1,2}\/\d{2}\/\d{4}).*?Amount to pay:.*\$(?<amount>[0-9]+(\.[0-9]{2})?)/',
+        'messageTransform' => function($message) {
+            return implode(" ", array_map('trim', explode("\n", strip_tags(base64_decode($message)))));
+        },
+        'dateTransform' => function($date) {
+            return str_replace('/', '-', $date);
+        },
+        'telegramMessage' => "Water bill: $%s each due on the %s"
+    ],
+    'Internet' => [
+        'imapQuery' => 'FROM "@online.telstra.com"',
+        'regex' => '/Total \$(?<amount>[0-9]+(\.[0-9]{2})?).*?Due Date (?<due>\d{1,2} \w{3} \d{4})/',
+        'messageTransform' => function($message) {
+            return implode(" ", array_map('trim', explode("\n", strip_tags(html_entity_decode($message)))));
+        },
+        'dateTransform' => 'Common\identity',
+        'telegramMessage' => "Internet bill: $%s each due on the %s"
+    ],
+    'Gas' => [
+        'imapQuery' => 'FROM "@energy.agl.com.au"',
+        'regex' => '/Direct Debit amount: \$(?<amount>[0-9]+(\.[0-9]{2})?).*?Direct Debit date: (?<due>\d{1,2} \w{3} \d+)/',
+        'messageTransform' => function($message) {
+            return implode(" ", array_map('trim', explode("\n", $message)));
+        },
+        'dateTransform' => 'Common\identity',
+        'telegramMessage' => "Gas bill: $%s each due on the %s"
+    ]
+];
diff --git a/wednesday.php b/wednesday.php
new file mode 100644 (file)
index 0000000..14dae41
--- /dev/null
@@ -0,0 +1,87 @@
+<?php declare(strict_types=1);
+
+require 'common.php';
+
+use function Common\getTelegram;
+use Telegram\Bot\Actions;
+
+$wednesdays = [
+    "9K4-jllrPrE",
+    "bbat6cvgEJ8",
+    "Oct2xKMGOno",
+    "DREDJ4fkz-g",
+    "gxm5SwfkwcI",
+    "oVxFk_IIB2o",
+    "SePVlroq6AI",
+    "JHO61_wDC30",
+    "EBNEPil4da0",
+    "pXv4zQ6dYPQ",
+    "hzGQSlrB1_o",
+    "Y_xlWdgi1ew",
+    "szqNmefKXxc",
+    "OzQ-KvxLVT0",
+    "zl6phK1mXC4",
+    "7aTtNNjIyi4",
+    "1CH-7qjz4D4",
+    "YSDAAh6Lps4",
+    "fyJGKEswuSc",
+    "csqJK8wwaHw",
+    "KSwnFzlPEuY",
+    "aew9WTLqjDc",
+    "m2Z0CyuyfMI",
+    "VaPMUACYWww",
+    "_87k7gxeVsw",
+    "3RSL5k3yZOM",
+    "VXc47lVx7Eo",
+    "0W51GIxnwKc",
+    "VfaNCw2bF48",
+    "It8RbsGIe48",
+    "NBPlPowAsNc",
+    "IaE0g3oVIZ0",
+    "VzigPnZ8OYE",
+    "meuYC7FP7HU",
+    "N3e7G9OxfhI",
+    "IR0QUwGmo4A",
+    "ESNBnxtpKqI",
+    "036ItQLi-sQ",
+    "Kz26jod9-cQ",
+    "LrleLDD8CJM",
+    "ZHS5yAwApUs",
+    "PE8GlPpuLuY",
+    "4Sr5pRpDZMk",
+    "qCsYa8PeVfU",
+    "-R40VcLKyIw",
+    "7dr2s59XnBE",
+    "iTl1l3GFMJ8",
+    "In9Bs1wiF5s",
+    "zHpFuOlPrlQ",
+    "Xf_wuAQ-t44",
+    "frNFBv2QIoE",
+    "PAnKl7862qc"
+];
+
+getTelegram()->sendMessage(
+    [
+        'chat_id' => CHAT_ID,
+        'parse_mode' => 'Markdown',
+        'text' => implode("\n", [
+            "```",
+            "🐸 MEME ALERT MY DUDES 🐸",
+            "  ** It is Wednesday **",
+            "      Meme Inbound",
+            "🐸 MEME ALERT MY DUDES 🐸",
+            "```"
+        ])
+    ]
+);
+
+getTelegram()->sendChatAction(['chat_id' => CHAT_ID, 'action' => Actions::TYPING]);
+
+sleep(2);
+
+getTelegram()->sendMessage(
+    [
+        'chat_id' => CHAT_ID,
+        'text' => 'https://www.youtube.com/watch?v=' . $wednesdays[(int)(new DateTimeImmutable)->format("W") - 1]
+    ]
+);