private string PreparePayload(string message, string title, string channel, SlackAttachment[] attachements) { //Prepare the string builder StringBuilder sb = new StringBuilder(); //Prepare the name string username = this.Name; if (title != "") { username += " - " + title; } if (this.Name == "") { username = title; } sb.Append(SlackHelper.SimpleJSONString("username", username)); sb.Append(", "); //Prepare the attachments if (attachements != null && attachements.Length > 0) { //Temporary holder of all attachments string attjson = ""; //Iterate over each attachment, adding it to the temp json foreach (SlackAttachment attachment in attachements) { attjson += (attjson.Length != 0 ? ", " : "") + attachment.GenerateJSON(); } //Append the temporary json to the main body sb.Append(SlackHelper.SimpleJSONValue("attachments", "[" + attjson + "]")); sb.Append(", "); } //Prepare the icon sb.Append(SlackHelper.SimpleJSONString(UsingEmoji() ? "icon_emoji" : "icon_url", this.Icon)); sb.Append(", "); //Prepare the channel if (channel == "") { channel = DefaultChannel; } sb.Append(SlackHelper.SimpleJSONString("channel", channel)); sb.Append(", "); //Prepare the message sb.Append(SlackHelper.SimpleJSONString("text", message)); sb.Append(", "); sb.Append(SlackHelper.SimpleJSONValue("mrkdown", UseMarkdown)); //return the formated json return("{" + sb.ToString() + "}"); }
static void Main(string[] args) { Console.WriteLine("Welcome to Slack Webhook Example! Creating Webhook and sending messages..."); //First prepare the webhook url and other parameters string webhookUrl = "https://hooks.slack.com/services/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"; string botname = "Robo Koala"; string boticon = ":koala:"; string channel = "@Lachee"; //This will be used later on for storing success results bool success = false; //Create the webhook SlackWebhook webhook = new SlackWebhook(webhookUrl, botname, boticon, channel); webhook.forceAsyncronus = false; //================================= Basic Sending //Send a simple message success = webhook.Send("`Hello World!`"); Console.WriteLine("Send: " + success); //Send a message with a title success = webhook.Send("Hello World!", "Welcome"); Console.WriteLine("Send Title: " + success); //Send a message to specific channel. Channels can be convos with @ or normal channels with # success = webhook.Send("Hello World!", "", "@Lachee"); //This is a standard approach success = webhook.Send("Hello World!", channel: "@Lachee"); //This is a approch where you can skip unneeded variables Console.WriteLine("Send Channel: " + success); //Send it all! success = webhook.Send("Hello World!", "Welcome", "@Lachee"); Console.WriteLine("Send All: " + success); //Here is a async example with a timmer to show that its continuing on. //Note while it is async, it still has a small initalization time due to HttpClient. var watch = System.Diagnostics.Stopwatch.StartNew(); success = webhook.SendAsync("Hellow World!", "Async"); watch.Stop(); Console.WriteLine("Async Call: " + success + " @ " + watch.ElapsedMilliseconds + "ms"); //================================= Some Formatting //Create a link to be used and send it string link = SlackHelper.CreateLink("http://google.com/", "Visit Google"); success = webhook.Send("Be sure to " + link + "! It can be very helpfull"); Console.WriteLine("Links: " + success + " - " + link); //================================= Create a basic attachment //Be sure to check out https://api.slack.com/docs/message-attachments for more details about each element. SlackAttachment attachement = new SlackAttachment("This is a attachment message", "Plaintext Fallback Message"); //You can do them indivdually like so: attachement.SetColor("#36a64f"); attachement.SetColor("danger"); attachement.SetPretext("some text that is before block"); //Or you can do it all inline! (Such Java like, much wow) attachement.SetTitle("When Dropbears Attack", "https://en.wikipedia.org/wiki/Drop_bear").SetFooter("Legit Koala Exhbit"); //Oh, don't forget to actually send it success = webhook.Send("Here is a interesting facts about dropbears", "News", attachments: new SlackAttachment[] { attachement }); Console.WriteLine("Attachments: " + success); Console.WriteLine("Done! Press any key to end."); Console.ReadKey(); }
/// <summary> /// Generates a JSON String repesentation of the attachment with Slack formatting. /// </summary> /// <returns></returns> public string GenerateJSON() { StringBuilder sb = new StringBuilder(); //Requirements sb.Append(SlackHelper.SimpleJSONString("fallback", this.fallback)); sb.Append(","); sb.Append(SlackHelper.SimpleJSONString("text", this.text)); sb.Append(","); sb.Append(SlackHelper.SimpleJSONValue("ts", this.timestamp)); sb.Append(","); //Slack fields //Prepare the field text string fieldjson = ""; foreach (SlackField field in fields) { string json = ""; json += SlackHelper.SimpleJSONString("title", field.title) + ","; json += SlackHelper.SimpleJSONString("value", field.text) + ","; json += SlackHelper.SimpleJSONValue("short", field.isShort); fieldjson += (fieldjson.Length != 0 ? ", " : "") + "{" + json + "}"; } //Append it sb.Append(SlackHelper.SimpleJSONValue("fields", "[" + fieldjson + "]")); sb.Append(","); //Formating and decoratives if (color != "") { sb.Append(SlackHelper.SimpleJSONString("color", this.color)); sb.Append(","); } if (pretext != "") { sb.Append(SlackHelper.SimpleJSONString("pretext", this.pretext)); sb.Append(","); } if (image_url != "") { sb.Append(SlackHelper.SimpleJSONString("image_url", this.image_url)); sb.Append(","); } if (thumb_url != "") { sb.Append(SlackHelper.SimpleJSONString("thumb_url", this.thumb_url)); sb.Append(","); } //Titles if (title != "") { sb.Append(SlackHelper.SimpleJSONString("title", this.title)); sb.Append(","); } if (title_link != "") { sb.Append(SlackHelper.SimpleJSONString("title_link", this.title_link)); sb.Append(","); } //Footer if (footer != "") { sb.Append(SlackHelper.SimpleJSONString("footer", this.footer)); sb.Append(","); } if (footer_icon != "") { sb.Append(SlackHelper.SimpleJSONString("footer_icon", this.footer_icon)); sb.Append(","); } //Author if (author_name != "") { sb.Append(SlackHelper.SimpleJSONString("author_name", this.author_name)); sb.Append(","); } if (author_link != "") { sb.Append(SlackHelper.SimpleJSONString("author_link", this.author_link)); sb.Append(","); } if (author_icon != "") { sb.Append(SlackHelper.SimpleJSONString("author_icon", this.author_icon)); sb.Append(","); } //Remove the last comma sb.Remove(sb.Length - 1, 1); return("{" + sb.ToString() + "}"); }