-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUtils.cs
45 lines (36 loc) · 1.47 KB
/
Utils.cs
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
using System.Collections.Generic;
using System.IO;
using System.Net;
using LitJson;
namespace DiscordNotifier
{
public class Utils
{
public static void PostMessage(string message, string username = null)
{
Main.StaticLogger.LogMessage($"Posting message to webhook: {message}");
var httpWebRequest = (HttpWebRequest) WebRequest.Create(Main.Configuration.WebhookUrl.Value);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var body = new Dictionary<string, string> {{"content", message}};
if (username != null) body.Add("username", username);
streamWriter.Write(JsonMapper.ToJson(body));
}
httpWebRequest.GetResponseAsync();
}
public static string FetchIPAddress()
{
string ipAddress;
const string url = @"https://api.ipify.org/";
var request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
using var response = (HttpWebResponse)request.GetResponse();
using var stream = response.GetResponseStream();
using var reader = new StreamReader(stream);
ipAddress = reader.ReadToEnd();
return ipAddress;
}
}
}