NWNX:EE  8193.36.12
nwnx_webhook_rch.nss
Go to the documentation of this file.
1 #include "nwnx_webhook"
5 
10  string sUsername;
11  string sText;
12  string sAvatarURL;
13  string sColor;
14  string sAuthorName;
15  string sAuthorURL;
16  string sAuthorIconURL;
17  string sTitle;
18  string sURL;
19  string sDescription;
20  string sThumbnailURL;
21  string sImageURL;
22  string sFooterText;
23  string sFooterURL;
24  int iTimestamp;
25  string sField1Name;
26  string sField1Value;
28  string sField2Name;
29  string sField2Value;
31  string sField3Name;
32  string sField3Value;
34  string sField4Name;
35  string sField4Value;
37  string sField5Name;
38  string sField5Value;
40  string sField6Name;
41  string sField6Value;
43  string sField7Name;
44  string sField7Value;
46  string sField8Name;
47  string sField8Value;
49  string sField9Name;
50  string sField9Value;
52  string sField10Name;
53  string sField10Value;
55 };
56 
61 string IntToBoolString(int iBool);
62 
70 string NWNX_WebHook_BuildMessageForWebHook(string host, string path, struct NWNX_WebHook_Message stMessage, int mrkdwn = 1);
71 
72 string IntToBoolString(int iBool)
73 {
74  return iBool == 0 ? "false" : "true";
75 }
76 
77 string NWNX_WebHook_BuildMessageForWebHook(string host, string path, struct NWNX_WebHook_Message stMessage, int mrkdwn = 1)
78 {
79  if (host == "discordapp.com" && GetStringRight(path, 6) != "/slack")
80  {
81  PrintString("Discord WebHook specified but path does not end with /slack");
82  return "";
83  }
84 
85  // Open JSON
86  string message = "{";
87 
88  string sMainText = "";
89 
90  // The only way to turn off markdown for discord is to surround the text in backticks
91  if (stMessage.sText != "")
92  {
93  if (host == "discordapp.com" && !mrkdwn)
94  sMainText = "```text\\n" + stMessage.sText + "```";
95  else
96  sMainText = stMessage.sText;
97  }
98  message = message + "\"text\": \"" + sMainText + "\"";
99 
100  // Slack will turn off markdown
101  if (host != "discordapp.com" && !mrkdwn)
102  message = message + ",\"mrkdwn\": false";
103 
104  // Set the user attributes for the poster
105  if (stMessage.sUsername != "")
106  message = message + ",\"username\": \"" + stMessage.sUsername + "\"";
107  if (stMessage.sAvatarURL != "")
108  message = message + ",\"icon_url\": \"" + stMessage.sAvatarURL + "\"";
109 
110  // We need to construct an attachment (embed) object
111  if (stMessage.sAuthorName != "" || stMessage.sAuthorURL != "" || stMessage.sAuthorIconURL != "" ||
112  stMessage.sTitle != "" || stMessage.sURL != "" || stMessage.sDescription != "" ||
113  stMessage.sFooterText != "" || stMessage.sFooterURL != "" || stMessage.iTimestamp > 0 ||
114  stMessage.sColor != "" || stMessage.sThumbnailURL != "" || stMessage.sImageURL != "" || stMessage.sField1Name != "")
115  {
116  message = message + ",\"attachments\": [{\"author_name\": \"" + stMessage.sAuthorName + "\",\"author_link\": \"" + stMessage.sAuthorURL +
117  "\",\"author_icon\": \"" + stMessage.sAuthorIconURL + "\",\"title\": \"" + stMessage.sTitle + "\",\"title_link\": \"" + stMessage.sURL +
118  "\",\"text\": \"" + stMessage.sDescription + "\",\"footer\": \"" + stMessage.sFooterText + "\",\"footer_icon\": \"" + stMessage.sFooterURL +
119  "\",\"color\": \"" + stMessage.sColor + "\",\"thumb_url\": \"" + stMessage.sThumbnailURL +
120  "\",\"image_url\": \"" + stMessage.sImageURL + "\"";
121 
122  // Dont post an empty timestamp
123  if (stMessage.iTimestamp > 0)
124  message = message + ",\"ts\": \"" + IntToString(stMessage.iTimestamp) + "\"";
125 
126  // Fields to handle
127  if (stMessage.sField1Name != "")
128  {
129  message = message + ",\"fields\": [";
130  message = message + "{\"title\": \"" + stMessage.sField1Name + "\",\"value\": \"" + stMessage.sField1Value + "\",\"short\": " + IntToBoolString(stMessage.iField1Inline) + "}";
131  if (stMessage.sField2Name != "")
132  message = message + ",{\"title\": \"" + stMessage.sField2Name + "\",\"value\": \"" + stMessage.sField2Value + "\",\"short\": " + IntToBoolString(stMessage.iField2Inline) + "}";
133  if (stMessage.sField3Name != "")
134  message = message + ",{\"title\": \"" + stMessage.sField3Name + "\",\"value\": \"" + stMessage.sField3Value + "\",\"short\": " + IntToBoolString(stMessage.iField3Inline) + "}";
135  if (stMessage.sField4Name != "")
136  message = message + ",{\"title\": \"" + stMessage.sField4Name + "\",\"value\": \"" + stMessage.sField4Value + "\",\"short\": " + IntToBoolString(stMessage.iField4Inline) + "}";
137  if (stMessage.sField5Name != "")
138  message = message + ",{\"title\": \"" + stMessage.sField5Name + "\",\"value\": \"" + stMessage.sField5Value + "\",\"short\": " + IntToBoolString(stMessage.iField5Inline) + "}";
139  if (stMessage.sField6Name != "")
140  message = message + ",{\"title\": \"" + stMessage.sField6Name + "\",\"value\": \"" + stMessage.sField6Value + "\",\"short\": " + IntToBoolString(stMessage.iField6Inline) + "}";
141  if (stMessage.sField7Name != "")
142  message = message + ",{\"title\": \"" + stMessage.sField7Name + "\",\"value\": \"" + stMessage.sField7Value + "\",\"short\": " + IntToBoolString(stMessage.iField7Inline) + "}";
143  if (stMessage.sField8Name != "")
144  message = message + ",{\"title\": \"" + stMessage.sField8Name + "\",\"value\": \"" + stMessage.sField8Value + "\",\"short\": " + IntToBoolString(stMessage.iField8Inline) + "}";
145  if (stMessage.sField9Name != "")
146  message = message + ",{\"title\": \"" + stMessage.sField9Name + "\",\"value\": \"" + stMessage.sField9Value + "\",\"short\": " + IntToBoolString(stMessage.iField9Inline) + "}";
147  if (stMessage.sField10Name != "")
148  message = message + ",{\"title\": \"" + stMessage.sField10Name + "\",\"value\": \"" + stMessage.sField10Value + "\",\"short\": " + IntToBoolString(stMessage.iField10Inline) + "}";
149  // Close fields array
150  message = message + "]";
151  }
152  // Close attachments array
153  message = message + "}]";
154  }
155  // Close JSON
156  message = message + "}";
157 
158  return message;
159 }
NWNX_WebHook_Message::sField1Name
string sField1Name
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:25
NWNX_WebHook_Message::iField3Inline
int iField3Inline
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:33
NWNX_WebHook_Message::sThumbnailURL
string sThumbnailURL
https://birdie0.github.io/discord-webhooks-guide/structure/embed/thumbnail.html
Definition: nwnx_webhook_rch.nss:20
NWNX_WebHook_Message::iField2Inline
int iField2Inline
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:30
NWNX_WebHook_Message::iField6Inline
int iField6Inline
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:42
NWNX_WebHook_Message::sField7Value
string sField7Value
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:44
NWNX_WebHook_Message::sFooterURL
string sFooterURL
https://birdie0.github.io/discord-webhooks-guide/structure/embed/footer.html
Definition: nwnx_webhook_rch.nss:23
NWNX_WebHook_Message::sFooterText
string sFooterText
https://birdie0.github.io/discord-webhooks-guide/structure/embed/footer.html
Definition: nwnx_webhook_rch.nss:22
NWNX_WebHook_Message::sField9Name
string sField9Name
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:49
NWNX_WebHook_Message::sField6Name
string sField6Name
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:40
NWNX_WebHook_Message::sField4Value
string sField4Value
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:35
NWNX_WebHook_Message::iField1Inline
int iField1Inline
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:27
NWNX_WebHook_Message::sField6Value
string sField6Value
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:41
NWNX_WebHook_Message::sUsername
string sUsername
https://birdie0.github.io/discord-webhooks-guide/structure/username.html
Definition: nwnx_webhook_rch.nss:10
NWNX_WebHook_Message
For more information on these fields see https://birdie0.github.io/discord-webhooks-guide/.
Definition: nwnx_webhook_rch.nss:9
NWNX_WebHook_Message::sField5Value
string sField5Value
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:38
NWNX_WebHook_Message::sField3Value
string sField3Value
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:32
NWNX_WebHook_Message::sTitle
string sTitle
https://birdie0.github.io/discord-webhooks-guide/structure/embed/title.html
Definition: nwnx_webhook_rch.nss:17
NWNX_WebHook_Message::iField8Inline
int iField8Inline
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:48
NWNX_WebHook_Message::sField8Name
string sField8Name
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:46
NWNX_WebHook_Message::iField9Inline
int iField9Inline
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:51
NWNX_WebHook_Message::sField2Value
string sField2Value
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:29
NWNX_WebHook_Message::sField10Name
string sField10Name
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:52
NWNX_WebHook_Message::sField3Name
string sField3Name
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:31
NWNX_WebHook_Message::sURL
string sURL
https://birdie0.github.io/discord-webhooks-guide/structure/embed/url.html
Definition: nwnx_webhook_rch.nss:18
NWNX_WebHook_Message::iField10Inline
int iField10Inline
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:54
NWNX_WebHook_Message::iField7Inline
int iField7Inline
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:45
NWNX_WebHook_Message::iTimestamp
int iTimestamp
https://birdie0.github.io/discord-webhooks-guide/structure/embed/timestamp.html
Definition: nwnx_webhook_rch.nss:24
NWNX_WebHook_Message::sField9Value
string sField9Value
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:50
NWNX_WebHook_Message::sText
string sText
https://birdie0.github.io/discord-webhooks-guide/structure/content.html
Definition: nwnx_webhook_rch.nss:11
NWNX_WebHook_Message::sField7Name
string sField7Name
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:43
NWNX_WebHook_Message::iField4Inline
int iField4Inline
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:36
NWNX_WebHook_Message::sField10Value
string sField10Value
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:53
NWNX_WebHook_Message::sField8Value
string sField8Value
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:47
NWNX_WebHook_BuildMessageForWebHook
string NWNX_WebHook_BuildMessageForWebHook(string host, string path, struct NWNX_WebHook_Message stMessage, int mrkdwn=1)
Builds and sends a rich webhook message based on the constructed NWNX_WebHook_Message.
Definition: nwnx_webhook_rch.nss:77
NWNX_WebHook_Message::sField1Value
string sField1Value
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:26
NWNX_WebHook_Message::sAuthorURL
string sAuthorURL
https://birdie0.github.io/discord-webhooks-guide/structure/embed/author.html
Definition: nwnx_webhook_rch.nss:15
NWNX_WebHook_Message::sField2Name
string sField2Name
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:28
NWNX_WebHook_Message::iField5Inline
int iField5Inline
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:39
NWNX_WebHook_Message::sField4Name
string sField4Name
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:34
NWNX_WebHook_Message::sAuthorIconURL
string sAuthorIconURL
https://birdie0.github.io/discord-webhooks-guide/structure/embed/author.html
Definition: nwnx_webhook_rch.nss:16
NWNX_WebHook_Message::sAuthorName
string sAuthorName
https://birdie0.github.io/discord-webhooks-guide/structure/embed/author.html
Definition: nwnx_webhook_rch.nss:14
NWNX_WebHook_Message::sColor
string sColor
https://birdie0.github.io/discord-webhooks-guide/structure/embed/color.html
Definition: nwnx_webhook_rch.nss:13
NWNX_WebHook_Message::sDescription
string sDescription
https://birdie0.github.io/discord-webhooks-guide/structure/embed/description.html
Definition: nwnx_webhook_rch.nss:19
NWNX_WebHook_Message::sImageURL
string sImageURL
https://birdie0.github.io/discord-webhooks-guide/structure/embed/image.html
Definition: nwnx_webhook_rch.nss:21
NWNX_WebHook_Message::sAvatarURL
string sAvatarURL
https://birdie0.github.io/discord-webhooks-guide/structure/avatar_url.html
Definition: nwnx_webhook_rch.nss:12
NWNX_WebHook_Message::sField5Name
string sField5Name
https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html
Definition: nwnx_webhook_rch.nss:37