Sample.Functions.SlackIniatedWebHook C# (CSharp) Method

SlackIniatedWebHook() public method

public SlackIniatedWebHook ( [ context, [ messages ) : Task
context [
messages [
return Task
        public async Task SlackIniatedWebHook([WebHookTrigger("slack/webhook")] WebHookContext context,
            [Queue("SlackWork")] ICollector<SlackWork> messages
        )
        {
            // Try and parse the Slack Message Body with simple helper method
            NameValueCollection nvc;
            if(TryParseSlackBody(await context.Request.Content.ReadAsStringAsync(), out nvc))
            {
                Regex rgx = new Regex("(\\d+) (\\d+)");
                Match match = rgx.Match(nvc["text"]);
                int count;
                int work;
                if(int.TryParse(match.Groups[1].Value, out count) && int.TryParse(match.Groups[2].Value, out work))
                {
                    for (int i = 0; i < count; i++)
                    {
                        messages.Add(new SlackWork { id = i, work = work, replyUrl = nvc["response_url"], username = nvc["user_name"] });
                    }

                    // All good, quickly send an affirmative response
                    context.Response = new HttpResponseMessage(HttpStatusCode.Accepted)
                    {
                        Content = new StringContent("Message received! Processing ...")
                    };
                }
                else
                {
                    // Not good, quick send a negative response
                    context.Response = new HttpResponseMessage(HttpStatusCode.Accepted)
                    {
                        Content = new StringContent("Incorrect format - please pass two numbers along - i.e. /cmd 2 30")
                    };

                    // We can stop here for the failure case
                    return;
                }
            }
            else
            {
                // Not good, quick send a negative response
                context.Response = new HttpResponseMessage(HttpStatusCode.Accepted)
                {
                    Content = new StringContent("Something went wrong. :(")
                };

                // We can stop here for the failure case
                return;
            }

            
        }