private async Task Say(BotMessage message, ResponseContext context) { string chatHubID = null; if (message.ChatHub != null) { chatHubID = message.ChatHub.ID; } else if (context != null && context.Message.ChatHub != null) { chatHubID = context.Message.ChatHub.ID; } if (chatHubID != null) { NoobWebClient client = new NoobWebClient(); await client.GetResponse( "https://slack.com/api/chat.postMessage", RequestType.Post, "token", this.SlackKey, "channel", chatHubID, "text", message.Text, "as_user", "true" ); } else { throw new ArgumentException("When calling the Say() method, the message parameter must have its ChatHub property set."); } }
private async Task Say(BotMessage message, ResponseContext context) { string chatHubID = null; if (message == null) { return; } if (message.ChatHub != null) { chatHubID = message.ChatHub.ID; } else if (context != null && context.Message.ChatHub != null) { chatHubID = context.Message.ChatHub.ID; } if (chatHubID == null) { throw new ArgumentException($"When calling the {nameof(Say)}() method, the {nameof(message)} parameter must have its {nameof(message.ChatHub)} property set."); } var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", SlackKey); dynamic values = new { token = SlackKey, channel = chatHubID, text = message.Text, as_user = true }; if (message.Attachments.Count > 0) { values.attachments = (JsonConvert.SerializeObject(message.Attachments)); } await client.PostAsync( "https://slack.com/api/chat.postMessage", new StringContent(JsonConvert.SerializeObject(values), System.Text.Encoding.UTF8, "application/json") ); if (message.File?.content != null) { message.File.content.Position = 0; var multiForm = new MultipartFormDataContent(); multiForm.Add(new StringContent(SlackKey), "token"); multiForm.Add(new StringContent(chatHubID), "channels"); multiForm.Add(new StringContent(message.File.title), "title"); multiForm.Add(new StreamContent(message.File.content), "file", message.File.filename); var response = await client.PostAsync("https://slack.com/api/files.upload", multiForm); } }
private async Task Say(BotMessage message, ResponseContext context) { string chatHubID = null; if (message == null) { return; } if (message.ChatHub != null) { chatHubID = message.ChatHub.ID; } else if (context != null && context.Message.ChatHub != null) { chatHubID = context.Message.ChatHub.ID; } if (chatHubID == null) { throw new ArgumentException($"When calling the {nameof(Say)}() method, the {nameof(message)} parameter must have its {nameof(message.ChatHub)} property set."); } var client = new NoobWebClient(); var values = new List <string>() { "token", SlackKey, "channel", chatHubID, "text", message.Text, "as_user", "true" }; if (message.Attachments.Count > 0) { values.Add("attachments"); values.Add(JsonConvert.SerializeObject(message.Attachments)); } /** CUSTOM ATTRIBUTE INSERTION ****************************************/ if (message.Thread_TS != null && message.Thread_TS != String.Empty) { values.Add("thread_ts"); values.Add(message.Thread_TS); } foreach (KeyValuePair <string, string> kv in message.Custom_Attibs) { values.Add(kv.Key); values.Add(kv.Value); } /** END CUSTOM ATTRIBS *************************************************/ await client.DownloadString( "https://slack.com/api/chat.postMessage", RequestMethod.Post, values.ToArray() ); }
private async Task Say(BotMessage message, ResponseContext context) { string chatHubID = null; if (message == null) { return; } if (message.ChatHub != null) { chatHubID = message.ChatHub.ID; } else if (context != null && context.Message.ChatHub != null) { chatHubID = context.Message.ChatHub.ID; } if (chatHubID != null) { NoobWebClient client = new NoobWebClient(); List <string> values = new List <string>() { "token", this.SlackKey, "channel", chatHubID, "text", message.Text, "as_user", "true" }; if (message.Attachments.Count > 0) { values.Add("attachments"); values.Add(JsonConvert.SerializeObject(message.Attachments)); } await client.DownloadString( "https://slack.com/api/chat.postMessage", RequestMethod.Post, values.ToArray() ); } else { throw new ArgumentException("When calling the Say() method, the message parameter must have its ChatHub property set."); } }
private async Task Say(BotMessage message, SlackMessage context) { string chatHubID = null; if (message == null) { return; } if (message.ChatHub != null) { chatHubID = message.ChatHub.ID; } else if (context != null && context.Message.ChatHub != null) { chatHubID = context.Message.ChatHub.ID; } if (chatHubID != null) { var client = new HttpClient(); List <KeyValuePair <string, string> > values = new List <KeyValuePair <string, string> >() { MakeKeyValue("token", this.SlackKey), MakeKeyValue("channel", chatHubID), MakeKeyValue("text", message.Text), MakeKeyValue("as_user", "true") }; if (message.Attachments.Count > 0) { values.Add(MakeKeyValue("attachments", JsonConvert.SerializeObject(message.Attachments))); } await client.PostAsync( "https://slack.com/api/chat.postMessage", new FormUrlEncodedContent(values) ); } else { throw new ArgumentException("When calling the Say() method, the message parameter must have its ChatHub property set."); } }
/// <summary> /// Allows you to programmatically operate the bot. The bot will post the passed message in whichever "chat hub" (DM, channel, or group) the message specifies. /// </summary> /// <param name="message">The message you want the bot to post in Slack.</param> public async Task Say(BotMessage message) { await Say(message, null); }