Skip to content

Commit 4e3aac3

Browse files
authored
feat: Proper test command (#4)
* feat: Add proper test command * refactor: Add periods and proper logs with test * chore(docs): Edit readme to include recent changes * chore(release): Rebuild binary
1 parent 8f65436 commit 4e3aac3

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ SourceMod plugin to allow sending logs.tf previews to a Discord channel through
2525
2. Restart the server or type `sm plugins load payload-webhook` in the console to load the plugin.
2626
3. The config file will be automatically generated in cfg/sourcemod/
2727
4. [Grab your webhook token](https://payload.tf/settings)
28-
5. Set the convar `payload_webhook_token` to the token gained above
28+
5. Set the convar `sm_payload_token` to the token gained above
2929
You're set! You should now recieve logs.tf previews when logs.tf uploads a log successfully.
3030

31-
If you wish to disable the previews, you can set the `payload_send` convar to `0`.
31+
If you wish to disable the previews, you can set the `sm_payload_send` convar to `0`.
3232

3333
# Issues, Questions
3434

compiled/payload-webhook.smx

387 Bytes
Binary file not shown.

scripting/payload-webhook.sp

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#pragma newdecls required
77
#pragma semicolon 1
88

9-
#define VERSION "1.0.1"
9+
#define VERSION "1.1.0"
1010

1111
char g_sWebhookToken[128];
1212

@@ -25,20 +25,48 @@ public Plugin myinfo =
2525

2626
public void OnPluginStart()
2727
{
28-
g_hCvarWebhookToken = CreateConVar("payload_webhook_token", "", "Channel to post rendered logs to", FCVAR_PROTECTED);
29-
g_hCvarApiUrl = CreateConVar("payload_apiurl", "https://api.payload.tf/api", "Payload API url", FCVAR_PROTECTED);
28+
g_hCvarWebhookToken = CreateConVar("sm_payload_token", "", "Channel to post rendered logs to", FCVAR_PROTECTED);
29+
g_hCvarApiUrl = CreateConVar("sm_payload_apiurl", "https://api.payload.tf/api", "Payload API url", FCVAR_PROTECTED);
3030

31-
g_hCvarSendLogs = CreateConVar("payload_send", "1", "Send the logs or not", FCVAR_NOTIFY);
31+
g_hCvarSendLogs = CreateConVar("sm_payload_send", "1", "Send the logs or not", FCVAR_NOTIFY);
3232

33-
RegConsoleCmd("payload_testupload", testUpload, "Debug function to simulate an uploaded log");
33+
RegConsoleCmd("sm_payload_test", TestUpload, "Debug function to simulate an uploaded log");
3434

3535
PrintToChatAll("[Payload] Plugin loaded.");
3636
PrintToServer("[Payload] Plugin loaded.");
3737
}
3838

39-
public Action testUpload(int client, int args)
39+
public Action TestUpload(int client, int args)
4040
{
41-
LogUploaded(true, "2961236", "https://logs.tf/2961236");
41+
bool sendRequest = GetConVarBool(g_hCvarSendLogs);
42+
if (sendRequest == false)
43+
{
44+
PrintToChatAll("[Payload] sm_payload_send is 0, not sending requests.");
45+
PrintToServer("[Payload] sm_payload_send is 0, not sending requests.");
46+
47+
return Plugin_Handled;
48+
}
49+
50+
// Make sure we update the string value of the token
51+
GetConVarString(g_hCvarWebhookToken, g_sWebhookToken, sizeof(g_sWebhookToken));
52+
53+
if (strlen(g_sWebhookToken) == 0)
54+
return Plugin_Handled;
55+
56+
char BaseUrl[64];
57+
char FullUrl[128];
58+
59+
// Store convar for the api Url in BaseUrl
60+
GetConVarString(g_hCvarApiUrl, BaseUrl, sizeof(BaseUrl));
61+
62+
// Complete the baseUrl
63+
Format(FullUrl, sizeof(FullUrl), "%s/webhooks/v1/internal/test", BaseUrl);
64+
65+
PrintToServer("[Payload] Testing webhook...");
66+
PrintToChatAll("[Payload] Testing webhook...");
67+
68+
SendTestRequest(FullUrl);
69+
4270
return Plugin_Handled;
4371
}
4472

@@ -88,18 +116,47 @@ public void SendRequest(const char[] logid, const char[] fullApiUrl)
88116
SteamWorks_SendHTTPRequest(hRequest);
89117
}
90118

119+
public void SendTestRequest(const char[] fullApiUrl)
120+
{
121+
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, fullApiUrl);
122+
123+
// Headers
124+
SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-Type", "x-www-form-urlencoded");
125+
SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Authorization", g_sWebhookToken);
126+
127+
SteamWorks_SetHTTPCallbacks(hRequest, TestWebhookComplete);
128+
SteamWorks_SendHTTPRequest(hRequest);
129+
}
130+
91131
public int OnSteamWorksHTTPComplete(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, any data)
92132
{
93133
if (bFailure)
94134
{
95-
PrintToChatAll("[Payload] Unable to post logs preview");
96-
PrintToServer("[Payload] Unable to post logs preview");
135+
PrintToChatAll("[Payload] Unable to post logs preview.");
136+
PrintToServer("[Payload] Unable to post logs preview.");
137+
PrintToServer("Status Code: %i", eStatusCode);
138+
}
139+
else
140+
{
141+
PrintToChatAll("[Payload] Log preview uploaded.");
142+
PrintToServer("[Payload] Log preview uploaded.");
143+
}
144+
145+
delete hRequest;
146+
}
147+
148+
public int TestWebhookComplete(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, any data)
149+
{
150+
if (bFailure)
151+
{
152+
PrintToChatAll("[Payload] Unable to test webhook.");
153+
PrintToServer("[Payload] Unable to test preview.");
97154
PrintToServer("Status Code: %i", eStatusCode);
98155
}
99156
else
100157
{
101-
PrintToChatAll("[Payload] Log preview uploaded");
102-
PrintToServer("[Payload] Log preview uploaded");
158+
PrintToChatAll("[Payload] Webhook test successful.");
159+
PrintToServer("[Payload] Webhook test successful.");
103160
}
104161

105162
delete hRequest;

0 commit comments

Comments
 (0)