-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTelegram.php
82 lines (71 loc) · 2.33 KB
/
Telegram.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
<?php
namespace Indeximstudio\Telegram;
use Indeximstudio\Telegram\components\InlineKeyboardButton;
use Indeximstudio\Telegram\components\ReplyMarkup;
class Telegram
{
public const TYPE_RECIPIENT = 'recipient';
public const TYPE_RECIPIENT_ADMIN = 'recipient_admin';
public $config;
private $bots;
private $replyMarkup;
function __construct($botName = '')
{
$this->bots = json_decode(evolutionCMS()->getConfig('client_telegramBots'), true);
$this->getBot($botName);
$this->replyMarkup = new ReplyMarkup();
}
/**
* @param $message
* @param string $type
*/
public function sendMessages($message, $type = self::TYPE_RECIPIENT)
{
$userList = explode(',', $this->config[$type]);
foreach ($userList as $index => $value) {
$this->sendMessage($message, trim($value));
}
}
/**
* @param $message
* @param $chat_id
*/
private function sendMessage($message, $chat_id)
{
if (is_array($message)) {
$message = json_encode([$this->getTitleMessage(), 'data' => $message], JSON_PRETTY_PRINT | JSON_UNESCAPED_LINE_TERMINATORS | JSON_UNESCAPED_SLASHES);
} elseif (is_object($message)) {
$message->site = $this->getTitleMessage();
} else {
$message = $this->getTitleMessage() . $message;
}
$message = urlencode($message);
$url = "https://api.telegram.org/bot{$this->config['token']}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$message}";
if ($this->replyMarkup->isNotEmpty()) {
$url.= "&reply_markup={$this->replyMarkup->toJson()}";
}
$ch = curl_init();
curl_setopt_array($ch, array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true));
curl_exec($ch);
curl_close($ch);
}
private function getBot($name)
{
foreach ($this->bots as $index => $value) {
if ($name == $value['bots']) {
return $this->config = $value;
}
}
return $this->config = $this->bots[0];
}
private function getTitleMessage()
{
global $modx;
return MODX_SITE_URL . '
';
}
public function addInlineButton(InlineKeyboardButton $button)
{
$this->replyMarkup->addInlineKeyboardButton($button);
}
}