-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_hook.php
160 lines (144 loc) · 4.64 KB
/
discord_hook.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
<?php
namespace app;
class DiscordHook
{
/* Question type constants */
const QT_GUESS_FROM_VIDEO = 0;
const QT_GUESS_VIDEO_FROM_WORD = 1;
const QT_TYPE_FROM_VIDEO = 2;
const QT_SIGN_FROM_WORD = 3;
const VERSION = 'discord_hook 1.0';
/**
* Merge categories into main categories
* (part before first / in category label)
*/
private static function merge_categories(array $list): array
{
$result = [];
foreach ($list as $item) {
$main_label = trim(explode('/', $item['category_label'])[0]);
if (!array_key_exists($main_label, $result)) {
$result[$main_label] = [
'ids' => [],
'label' => $main_label,
'count' => 0,
];
}
$result[$main_label]['ids'][] = $item['category_id'];
$result[$main_label]['count'] += $item['count'];
}
return $result;
}
/**
* Get a random key into list with string keys
*/
private static function random_key(array $list): string
{
$keys = array_keys($list);
$i = random_int(0, count($keys) - 1);
return $keys[$i];
}
/**
* Generate random question
* @param array $category_ids Array of category ids to include
* @param int $n_answers Number of answers to include
*/
private static function generate_question(array $category_ids, int $n_answers): array
{
$type = random_int(0, 3);
if (in_array($type, [self::QT_TYPE_FROM_VIDEO, self::QT_SIGN_FROM_WORD])) {
$n_answers = 1;
}
$correct_index = random_int(0, $n_answers - 1);
$result = [
'type' => $type,
'correct_index' => $correct_index,
'words' => [],
];
$words = json_decode(file_get_contents(
'https://tspquiz.se/api/?action=random' .
'&count=' . urlencode($n_answers) .
'&excludeUncommon=1' .
'&categories=' . urlencode('[' . implode(',', $category_ids) . ']')), true);
if (!is_array($words)) {
throw new \Exception('Loading words failed');
}
foreach ($words as $word) {
$result['words'][] = $word['id'];
}
return $result;
}
/**
* Generate random quiz
* @param array $category_ids Array of category ids to include
*/
private static function generate_quiz(array $category_ids): array
{
$n_question = random_int(5, 10);
$n_answers = random_int(3, 5);
$result = [
'version' => 1,
'questions' => [],
];
for ($i = 0; $i < $n_question; $i++) {
$type = random_int(0, 3);
$result['questions'][] = self::generate_question($category_ids, $n_answers);
}
return $result;
}
/**
* Create share link for given quiz data
*/
private static function share_link(array $quiz): string
{
$data_str = json_encode($quiz);
$base64_str = base64_encode($data_str);
$url = 'https://tspquiz.se/app' .
'?loadQuiz=' . urlencode($base64_str) .
'#/start';
return $url;
}
private static function compose_discord_message(array $quiz, string $category_label): string
{
$share_link = self::share_link($quiz);
$n_questions = count($quiz['questions']);
return json_encode([
'username' => 'TSP Quiz',
'content' => "$n_questions frågor om $category_label",
'embeds' => [
[
'title' => 'Öppna i TSP Quiz',
'description' => '',
'url' => $share_link,
],
],
]);
}
private static function discord_post(string $msg, string $webhook): void
{
$ch = curl_init($webhook);
$data = 'payload_json=' . urlencode($msg).'';
if(isset($ch)) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
}
}
/**
* Generate random quiz and post to discord using given webhook
*/
public static function run($webhook): void
{
$category_list = json_decode(file_get_contents(
'https://tspquiz.se/api/?action=list-category' .
'&appVersion='.urlencode(self::VERSION)), true);
$main_category_list = self::merge_categories($category_list);
$random_category = $main_category_list[self::random_key($main_category_list)];
$quiz = self::generate_quiz($random_category['ids']);
$msg = self::compose_discord_message($quiz, $random_category['label']);
self::discord_post($msg, $webhook);
}
}