forked from howar31/PHP-Discord-Webhooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordWebhooks.php
113 lines (100 loc) · 3.32 KB
/
DiscordWebhooks.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
<?php
require_once(dirname(__FILE__).'/config.php');
class DiscordWebhooks
{
protected static $action = '';
protected static $agent = 'PHP-DISCORD-WEBHOOKS-by-Howar31';
protected static function cPost (array $params = array())
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HEADER => 0,
CURLOPT_POST => true,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => static::$agent,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_POSTFIELDS => json_encode($params),
CURLOPT_URL => API_URL.static::$action,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
protected static function cGet (array $params = array())
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HEADER => 0,
CURLOPT_POST => false,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => static::$agent,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_URL => API_URL.static::$action.'?'.http_build_query($params),
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* Execute Webhook
* https://discordapp.com/developers/docs/resources/webhook#execute-webhook
* @param array $input array contain all input parameters:
* content: the message contents (up to 2000 characters)
* username: override the default username of the webhook
* avatar_url: override the default avatar of the webhook
* tts: true if this is a TTS message
* file: the contents of the file being sent
* embeds: embedded rich content
* @return array return:
* success: execution success or not
* error: error message if failed
*/
public function execute (array $input = array())
{
$return = array(
'success' => false,
'error' => ''
);
if (empty($input))
{
$return['error'] = 'Input is empty';
goto END;
}
if (empty($input['content']))
{
$return['error'] = 'Input content is empty';
goto END;
}
static::$action = 'webhooks/'.WEBHOOK_ID.'/'.WEBHOOK_TOKEN;
$data = array(
'content' => trim($input['content']),
'username' => empty($input['username']) ? '' : $input['username'],
'avatar_url' => empty($input['avatar_url']) ? '' : $input['avatar_url'],
'tts' => empty($input['tts']) ? false : $input['tts'],
'file' => empty($input['file']) ? '' : $input['file'],
'embeds' => (!empty($input['embeds']) && is_array($input['embeds'])) ? $input['embeds'] : array()
);
$result = $this->cPost($data);
if (!IS_LIVE && !empty($result))
{
$return['error'] = $result;
goto END;
}
END:
$return['success'] = empty($return['error']) ? true : false;
return $return;
}
}
?>