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() + "}"); }
/// <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() + "}"); }