Skip to content

Commit 84caf4e

Browse files
committed
Create DiscordWebhook-v7.0.php
Created new version that is working with PHP version 7.0.
1 parent 68375e6 commit 84caf4e

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

DiscordWebhook-v7.0.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Discord Webhook
4+
* v1.0.1
5+
* Updated: 17.07.2019
6+
* www.magictm.com
7+
* (c) Marcin Stawowczyk 2019
8+
* License: MIT
9+
*/
10+
final class DiscordWebhook
11+
{
12+
private $msg;
13+
private $url;
14+
private $username;
15+
private $avatar;
16+
private $tts;
17+
/**
18+
* @param string $msg message to send
19+
* @param string $url Discord Webhook URL
20+
* @param string|null $username username
21+
* @param string|null $avatar bot avatar
22+
* @param boolean $tts text-to-speech - the message will be readed
23+
*/
24+
public function __construct(
25+
string $msg,
26+
string $url = null,
27+
string $username = null,
28+
string $avatar = null,
29+
bool $tts = null
30+
)
31+
{
32+
$this->msg = $msg;
33+
$this->url = $url ??
34+
'https://discordapp.com/api/webhooks/536576089582075936/QkY4y6xDDlrPXXtnSXLiAOnIo961w6BXl1SLTE0bj-t0--A-P3thhos5tskW_lIhiRfG';
35+
$this->username = $username ?? 'www.magictm.com';
36+
$this->avatar = $avatar ??
37+
'https://i.ytimg.com/vi/JrQkgLLL9XQ/hqdefault.jpg';
38+
$this->tts = $tts ?? false;
39+
}
40+
41+
public function tts(
42+
bool $tts
43+
)
44+
{
45+
$this->tts = $tts;
46+
}
47+
48+
public function send()
49+
{
50+
$curl = curl_init();
51+
//timeouts - 5 seconds
52+
curl_setopt($curl, CURLOPT_TIMEOUT, 5); // 5 seconds
53+
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); // 5 seconds
54+
curl_setopt($curl, CURLOPT_URL, $this->url);
55+
curl_setopt($curl, CURLOPT_POST, 1);
56+
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
57+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
58+
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
59+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
60+
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
61+
'content' => $this->msg,
62+
'username' => $this->username,
63+
'avatar_url' => $this->avatar,
64+
'tts' => $this->tts
65+
]));
66+
$output = json_decode(
67+
curl_exec($curl),
68+
true
69+
);
70+
71+
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != 204) {
72+
curl_close($curl);
73+
$message = "";
74+
foreach ($output as $key => $value) {
75+
$message .= ucfirst($key) . " - " .$value[0];
76+
}
77+
throw new Exception("Wystąpił błąd podczas przesyłania wiadomości na Discorda: " . $message);
78+
}
79+
curl_close($curl);
80+
}
81+
}
82+
83+
?>

0 commit comments

Comments
 (0)