Coding style fix
[SonOfLokstallBot.git] / purjolok.php
1 <?php declare(strict_types=1);
2
3 require_once('common.php');
4
5 use Telegram\Bot\Actions;
6 use Telegram\Bot\Commands\Command;
7
8 if(!canChatWith(getTelegram()->getWebhookUpdates())) {
9 getTelegram()->sendMessage([
10 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
11 'text' => "Sorry, Dad says I can't talk to you."
12 ]);
13 exit(0);
14 }
15
16 getTelegram()->addCommand(
17 new class extends Command {
18 protected $name = 'searchmovies';
19 protected $description = 'Search through the movie collection';
20
21 public function handle($arguments) {
22 $top5 =(unlines, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort((f(field('similarity')),, field('similarity'))));
23 $movies = map(function($movie) use ($arguments) {
24 similar_text($arguments, substr($movie, 0, -7) ?: '', $perc);
25 return ['title' => $movie, 'similarity' => $perc];
26 })(scandir('/mnt/media/Movies'));
27
28 $this->replyWithMessage(
29 [
30 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies)
31 ]
32 );
33 }
34 }
35 );
36
37 getTelegram()->addCommand(
38 new class extends Command {
39 protected $name = 'chatid';
40 protected $description = 'Get the id for this chat.';
41
42 public function handle($arguments) {
43 $this->replyWithMessage([
44 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
45 ]);
46 }
47 }
48 );
49
50 getTelegram()->addCommand(
51 new class extends Command {
52 protected $name = 'mybills';
53 protected $description = 'List my bills';
54
55 public function handle($arguments) {
56 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
57 $this->replyWithChatAction(['action' => Actions::TYPING]);
58 $this->replyWithMessage([
59 'text' => implode(
60 "\n",
61 array_map(
62 function($bill) {
63 return sprintf(
64 "%s (%s): $%s each due on the %s",
65 $bill['service'],
66 $bill['id'],
67 splitBill($bill['amount']),
68 $bill['due']->format('jS \of M')
69 );
70 },
71 getMessagesFromInbox(
72 getInbox(
73 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
74 ),
75 require 'rules.php',
76 FALSE
77 )
78 ) ?: ['You have no unpaid bills! Nice one.']
79 )
80 ]);
81 }
82 }
83 );
84
85 getTelegram()->addCommand(
86 new class extends Command {
87 protected $name = 'paybill';
88 protected $description = 'Mark a bill as paid';
89
90 public function handle($arguments) {
91 if (!$arguments) {
92 $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..."]);
93 $this->triggerCommand('mybills');
94 }
95
96 $messages = array_values(array_filter(
97 getMessagesFromInbox(
98 getInbox(
99 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
100 ),
101 require 'rules.php',
102 FALSE
103 ),
104 function($e) use ($arguments) {
105 return $e['id'] == $arguments;
106 }
107 ));
108
109 if(!$messages || count($messages) !== 1) {
110 $this->replyWithMessage(['text' => "That doesn't look like a valid id. Use /mybills to list your bills."]);
111 }
112
113 imap_delete(getInbox('Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'), $messages[0]['uid'], FT_UID);
114 $this->replyWithMessage(['text' => "I marked " . getMessageSenderDisplayName($this->getUpdate()) . " as having paid the " . strtolower($messages[0]['service']) . " bill, thanks!"]);
115 }
116 }
117
118 );
119
120 getTelegram()->commandsHandler(true); //must come afterwards because lolzer