-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusPlusBot.php
More file actions
94 lines (75 loc) · 3.96 KB
/
PlusPlusBot.php
File metadata and controls
94 lines (75 loc) · 3.96 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
<?php
/*
* Made by @kaneki666 on Telegram!
*/
// Include the framework
require './vendor/autoload.php';
require 'database.php';
// Create a bot
$bot = new PhpBotFramework\Bot("BOT_TOKEN");
//Create pdo object
$bot->database->pdo = new PDO('mysql:host=localhost;dbname=DATABASE_NAME;charset=utf8mb4', 'DATABASE_USER', 'DATABASE_PASSWORD');
// Create a Database object
$db = new Database($bot->getPdo());
// start command.
$bot->addMessageCommand('start', function($bot, $message) {
$user_id = $message['from']['id'];
$username = $message['from']['username'];
global $db;
if ($username) {
$db->addUser($user_id, $username);
$bot->sendMessage("PlusPlus++ allows you to plus, minus and keep score of all the good and not so good things your friends say and do on Telegram.
You can add a point by typing @user++, deduct a point by typing @user-- and check the leaderboard by typing /leaderboard.");
} else {
$bot->sendMessage("You need an username to use this bot. Please set one from Telegram options");
}
});
$bot->answerUpdate['message'] = function ($bot, $message) {
global $db;
$user_id = $message['from']['id'];
$chat_id = $message['chat']['id'];
if ($message['from']['username']) {
foreach ($message['entities'] as $entity) {
if ($entity['type'] == 'mention') {
if (substr($chat_id, 0, 1) == "-") {
if ($db->getUser(substr($message['text'], $entity['offset'], $entity['length'])) == true) {
if (substr($message['text'], $entity['offset'] + $entity['length'], 2) == "++") {
//this add a point
$db->addPointToUser($chat_id, substr($message['text'], $entity['offset'], $entity['length']));
$bot->sendMessage("You added a point to " . substr($message['text'], $entity['offset'], $entity['length']));
} elseif (substr($message['text'], $entity['offset'] + $entity['length'], 2) == "--") {
//this deduct a point
$db->deductPointToUser($chat_id, substr($message['text'], $entity['offset'], $entity['length']));
$bot->sendMessage("You deduct a point to " . substr($message['text'], $entity['offset'], $entity['length']));
} else {
//when there is no ++ or -- after the mention
$bot->sendMessage("Invalid action for " . substr($message['text'], $entity['offset'], $entity['length']));
}
} else {
//if the username you want to vote isn't in the database
$bot->sendMessage("Username not found, please ask " . substr($message['text'], $entity['offset'], $entity['length']) . " to start the bot");
}
} else {
$bot->sendMessage("You can't use this bot in a private chat, please add it to a group");
}
}
}
//add user to database
$db->addUser($user_id, $message['from']['username']);
} else if (isset($message['entities']) and !isset($message['from']['username'])){
//if the user doesn't have an username and try to vote
$bot->sendMessage("Please set an username to use this bot");
}
};
//Help message
$bot->addMessageCommand('help', function ($bot, $message){
$bot->sendMessage("PlusPlus++ allows you to plus, minus and keep score of all the good and not so good things your friends say and do on Telegram.
You can add a point by typing @user++, deduct a point by typing @user-- and check the leaderboard by typing /leaderboard.");
});
$bot->addMessageCommand('leaderboard', function ($bot, $message){
global $db;
$leaderboard = $db->getLeaderboard($message['chat']['id']);
$bot->sendMessage($leaderboard);
});
// Receive updates from Telegram using getUpdates
$bot->getUpdatesLocal();