-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.php
More file actions
95 lines (81 loc) · 3.18 KB
/
Bot.php
File metadata and controls
95 lines (81 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
class Bot{
const CHAT_NAME = '#oleg.roshnivskyy/$aleksandr.subota;29cc0920f1150a6b';
private $last_id;
private $proxy;
private $votes;
private $firstVotedTime;
public function getVotes(){
return $this->votes;
}
public function clearVoting($chat){
unset($this->votes[$chat]);
}
public function __construct( $proxy ){
$this->proxy = $proxy;
}
public function invoke( $message_id ){
list( $chat, $message, $message_id, $author ) = $this->getDetails( $message_id );
$this->reply( $chat, $message, $message_id, $author );
}
public function getDetails( $message_id ){
$ch = $this->proxy->Invoke( 'GET CHATMESSAGE ' . $message_id . ' CHATNAME' ); //Get chat id
$mess = $this->proxy->Invoke( 'GET CHATMESSAGE ' . $message_id . ' BODY' ); //Get message text
$aut = $this->proxy->Invoke( 'GET CHATMESSAGE ' . $message_id . ' FROM_DISPNAME' ); //Get message author
$author = explode( 'FROM_DISPNAME ', $aut );
$chat = explode( 'CHATNAME ', $ch );
$message = explode( 'BODY ', $mess );
return [ $chat[ 1 ], $message[ 1 ], $message_id, $author[ 1 ] ];
}
public function reply( $chat, $message, $id, $author ){
// echo $chat,"\n";
if($chat != self::CHAT_NAME){
return;
}
$botReply = "[BOT]: ";
$reply = '';
if($id==$this->last_id){
return;
}else{
$this->last_id = $id;
}
switch ( $message ){
case '!clear':
$reply = "Votes were erased!";
$this->clearVoting($chat);
break;
case 'football':
case 'go':
case '+1':
case '+':
if ( !isset( $this->votes[$chat] ) ){
$reply = $author . ' had started voting';
$this->votes[$chat] = array( 'time'=>time(), "votes" => array( $author => $message) );
$this->firstVotedTime = time();
} else if ( isset( $this->votes[$chat]["votes"][ $author ] ) ){
$reply = "You already voted!";
break;
} else{
$this->votes[$chat]["votes"][ $author ] = $message;
$reply = "Vote was added";
}
if ( count( $this->votes[$chat]["votes"] ) >= 4 ){
$reply = "Got a quorum, " . implode( " ", array_keys( $this->votes[$chat]["votes"] ) ) . " lets play a football!";
$this->clearVoting($chat);
} else{
$reply .= ".\n Need " . ( 4 - count( $this->votes[$chat]["votes"] ) ) . " votes to make a quorum!";
}
break;
case '!test':
$reply = 'It\'s work!';
break;
case '!help':
$reply = 'Use football, go, + or +1';
break;
}
if ( isset($reply) && $reply != '' ){
$replyMessage = $botReply . $reply;
$this->proxy->Invoke( 'CHATMESSAGE ' . $chat . ' ' . $replyMessage );
} //Send message
}
}