-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.php
163 lines (144 loc) · 5.88 KB
/
bot.php
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
include 'env.php';
require 'encoding.class.php';
if(!defined('__TGWEBHOOKCALL__'))
{
header('HTTP/1.1 401 Unauthorized');
die();
}
function sendMessage($chatId, $message, $logfh = null)
{
if(!($logfh === null))
fwrite($logfh, 'Attempting to message '.$chatId.' with '.$message.PHP_EOL);
$url = TG_BOTAPI_URL.'/sendMessage?chat_id='.$chatId.'&text='.urlencode(Encoding::fixUTF8(utf8_encode($message)));
$res = file_get_contents($url);
if(!($logfh === null))
fwrite($logfh, 'Result was '.$res.PHP_EOL);
}
function parseChatFile($filePath)
{
if(!file_exists($filePath))
return [];
else
return json_decode(file_get_contents($filePath));
}
function saveChatFile($dataArr, $filePath)
{
$fh = fopen($filePath, 'w');
if(!$fh)
return false;
if(!fwrite($fh, json_encode($dataArr)))
return false;
fclose($fh);
return true;
}
function registerParticipation($chatID, $username)
{
$chatFile = './chats/'.str_replace('/', "", $chatID).'.json';
$chatData = parseChatFile($chatFile);
$numParticipants = count($chatData);
if($numParticipants > 4)
return 'Sorry, full party already';
if(substr($username, 0, 1) === '@')
$username = substr($username, 1);
if(empty($username))
return 'Having no username is shameful. I do not permit you to sign up without one.';
if(in_array($username, $chatData))
return 'Already registered';
else
{
$chatData[] = $username;
if(saveChatFile($chatData, $chatFile))
return ($numParticipants+1).'/5 (@'.$username.')';
else
return 'Error registering user '.$username;
}
}
function status($chatID)
{
$chatFile = './chats/'.str_replace('/', "", $chatID).'.json';
$chatData = parseChatFile($chatFile);
$numParticipants = count($chatData);
if($numParticipants < 1)
return 'No party is currently on';
$res = 'Current party size is '.$numParticipants.'/5:'.PHP_EOL;
foreach($chatData as $participant)
$res .= '@'.$participant.PHP_EOL;
return $res;
}
function resign($chatID, $username)
{
$chatFile = './chats/'.str_replace('/', "", $chatID).'.json';
$chatData = parseChatFile($chatFile);
$newChatData = [];
foreach($chatData as $participant)
if($participant !== $username)
$newChatData[] = $participant;
$res = saveChatFile($newChatData, $chatFile);
if($res)
return 'If you were in the party, you have now been removed';
else
return 'Error resigning';
}
function resetParty($chatID)
{
$chatFile = './chats/'.str_replace('/', "", $chatID).'.json';
$res = unlink($chatFile);
if($res)
return 'Party discarded.';
else
return 'Problem discarding party.';
}
function addParticipant($chatID, $username)
{
if(empty($username))
return 'No username supplied.';
return registerParticipation($chatID, $username);
}
function terminateParticipant($chatID, $username)
{
if(empty($username))
return 'No username supplied.';
$chatFile = './chats/'.str_replace('/', "", $chatID).'.json';
$chatData = parseChatFile($chatFile);
if(!in_array($username, $chatData))
return $username.' is not participating.';
$res = resign($chatID, $username);
if($res == 'Error resigning')
return 'Error terminating @'.$username;
return '@'.$username.' terminated.';
}
$fh = fopen('webhookpost.log', 'a');
if(!$fh)
trigger_error('Unable to open webhookpost.log for appending', E_USER_ERROR);
fwrite($fh, date('[d.m.Y H:i:s] ').'Webhook triggered.'.PHP_EOL);
$reqJson = file_get_contents('php://input');
#fwrite($fh, $reqJson.PHP_EOL);
$reqData = json_decode($reqJson, true);
fwrite($fh, print_r($reqData, true));
#fwrite($fh, PHP_EOL);
$msgRcvd = $reqData['message']['text'];
$chatID = $reqData['message']['chat']['id'];
$botName = 'axynd2bot';
$lowerMsgRcvd = strtolower($msgRcvd);
$lowerSplitted = explode(' ', $msgRcvd);
if($lowerMsgRcvd == '/d2' || $lowerMsgRcvd == '/d2@'.$botName || $lowerMsgRcvd == '/signmeupfam' || $lowerMsgRcvd == '/signmeupfam@'.$botName)
sendMessage($chatID, registerParticipation($reqData['message']['chat']['id'], $reqData['message']['from']['username']));
else if($lowerMsgRcvd == '/status' || $lowerMsgRcvd == '/status@'.$botName)
sendMessage($chatID, status($chatID));
else if($lowerMsgRcvd == '/reset' || $lowerMsgRcvd == '/reset@'.$botName)
sendMessage($chatID, resetParty($reqData['message']['chat']['id']));
else if($lowerMsgRcvd == '/resign' || $lowerMsgRcvd == '/resign@'.$botName)
{
sendMessage($chatID, resign($chatID, $reqData['message']['from']['username']));
sendMessage($chatID, status($chatID));
}
else if($lowerSplitted[0] == '/add' || $lowerSplitted[0] == '/add@'.$botName)
sendMessage($chatID, addParticipant($chatID, $lowerSplitted[1]));
else if($lowerSplitted[0] == '/terminate' || $lowerSplitted[0] == '/terminate@'.$botName)
sendMessage($chatID, terminateParticipant($chatID, $lowerSplitted[1]));
else if($lowerSplitted[0] == '/_')
sendMessage($chatID, 'Stop it, get some help.');
/*else
fwrite($fh, "Ei tajuu: ".$msgRcvd.PHP_EOL);*/
fclose($fh);