-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiscordWebhook.cpp
32 lines (26 loc) · 1.31 KB
/
DiscordWebhook.cpp
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
#include "DiscordWebhook.h"
void UDiscordWebhook::SendDiscordMessage(const FString& Webhook, const FString& Message)
{
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = FHttpModule::Get().CreateRequest();
Request->OnProcessRequestComplete().BindStatic(&UDiscordWebhook::OnResponseReceived); // Use BindStatic for static functions
Request->SetURL(Webhook);
Request->SetVerb("POST");
Request->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
Request->SetHeader("Content-Type", TEXT("application/json"));
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
JsonObject->SetStringField(TEXT("content"), Message);
FString JsonString;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&JsonString);
FJsonSerializer::Serialize(JsonObject.ToSharedRef(), Writer);
Request->SetContentAsString(JsonString);
Request->ProcessRequest();
}
void UDiscordWebhook::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (!bWasSuccessful || !EHttpResponseCodes::IsOk(Response->GetResponseCode()))
{
UE_LOG(LogTemp, Warning, TEXT("Failed to send message to Discord"));
return;
}
UE_LOG(LogTemp, Log, TEXT("Successfully sent message to Discord"));
}