Last active
June 23, 2019 23:45
-
-
Save ahelland/484c267a61c11255bed5e00c014c3713 to your computer and use it in GitHub Desktop.
Azure Function for receiving and processing an outbound WebHook from Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #r "Newtonsoft.Json" | |
| #r "System.Web.Extensions" | |
| #r "System.Web" | |
| using System; | |
| using System.Net; | |
| using Newtonsoft.Json; | |
| using Newtonsoft.Json.Linq; | |
| using System.Web; | |
| using System.Web.Script.Serialization; | |
| using System.Linq; | |
| public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) | |
| { | |
| // Get request body and parse it into json | |
| string bodyContent = await req.Content.ReadAsStringAsync(); | |
| var values = HttpUtility.ParseQueryString(bodyContent); | |
| var jsonContent = new JavaScriptSerializer() | |
| .Serialize(values.AllKeys.ToDictionary(k => k, k => values[k])); | |
| log.Info(jsonContent); | |
| var incoming = JsonConvert.DeserializeObject<SlackRequest>(jsonContent); | |
| var responseMsg = String.Empty; | |
| if (incoming.text == "bender " || incoming.text == "bender") | |
| { | |
| responseMsg = "What do you want?"; | |
| } | |
| else | |
| { | |
| Random r = new Random(); | |
| int rInt = r.Next(0,9); | |
| responseMsg = GetQuote(rInt); | |
| } | |
| var msg = new SlackResponse{text = $"{responseMsg}"}; | |
| return req.CreateResponse(HttpStatusCode.OK, msg); | |
| } | |
| public class SlackResponse | |
| { | |
| public string text {get;set;} | |
| } | |
| public class SlackRequest | |
| { | |
| public string token{get;set;} | |
| public string team_id{get;set;} | |
| public string team_domain{get;set;} | |
| public string channel_id{get;set;} | |
| public string channel_name{get;set;} | |
| public string timestamp{get;set;} | |
| public string user_id{get;set;} | |
| public string user_name{get;set;} | |
| public string text{get;set;} | |
| public string trigger_word{get;set;} | |
| } | |
| public static string GetQuote(int i) | |
| { | |
| switch (i) | |
| { | |
| case 0: | |
| return "Who are you, and why should I care?"; | |
| case 1: | |
| return "Hasta la vista, meatbag!"; | |
| case 2: | |
| return "Do the Bender! Do the Bender! It's your birthday! Do the Bender!"; | |
| case 3: | |
| return "Bite my shiny metal ass!"; | |
| case 4: | |
| return "Well, we're boned."; | |
| case 5: | |
| return "Hey sexy mama, wanna kill all humans?"; | |
| case 6: | |
| return "Too bad losers whom I've always hated!"; | |
| case 7: | |
| return "Would you kindly shut your noise hole?"; | |
| case 8: | |
| return "Get up and pay attention to me; Bender!"; | |
| case 9: | |
| return "Well, that was dumb..."; | |
| default: | |
| return "Oh noes, out of quotes!"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment