-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
87 lines (72 loc) · 2.01 KB
/
index.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
/**
* Method which actually sends a message to Telegram
* @param array $message
* @return void
*/
function send($message) {
$message = $message['attachments'][0];
$content = '<a href="' . htmlspecialchars($message['title_link'], ENT_QUOTES) . '">' . $message['title'] . '</a>: ' . $message['fallback'];
$data = createMessagePayload($content);
$result = Request::sendMessage($data);
}
/**
* Checks wether the Connector works with the current settings
* @return boolean
*/
function works() {
$data = createMessagePayload('Flarum is able to contact this Telegram room. Great!');
try {
$result = Request::sendMessage($data);
return $result->isOk();
} catch (TelegramException $e) {
return false;
}
}
/**
* Creates a payload to be sent using the API.
* @param string $message
* @return array
*/
function createMessagePayload($message) {
global $config;
return [
'chat_id' => $config['chatId'],
'text' => $message,
'parse_mode' => 'HTML',
];
}
$credentials = $_ENV['CREDENTIALS_DIRECTORY'] ?? null;
if ($credentials !== null) {
$config = [
'apiKey' => file_get_contents($credentials . '/apiKey'),
'botName' => file_get_contents($credentials . '/botName'),
'chatId' => file_get_contents($credentials . '/chatId'),
'token' => file_get_contents($credentials . '/token'),
];
} else {
$config = require __DIR__ . '/config.php';
}
if (!isset($_GET['token']) || $_GET['token'] !== $config['token']) {
http_response_code(403);
echo 'Invalid webhook token' . PHP_EOL;
die;
}
if (isset($_GET['action'])) {
$telegram = new Telegram($config['apiKey'], $config['botName']);
if ($_GET['action'] === 'test') {
works();
die;
} elseif ($_GET['action'] === 'hook') {
$json = file_get_contents('php://input');
$message = json_decode($json, TRUE);
send($message);
die;
}
}
http_response_code(400);
echo 'Unknown action' . PHP_EOL;