예제 #1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            log.Info($"{nameof(CSharpCompilerWebhookCSharp)} : C# HTTP trigger function processed a request.");

            string jsonContent = await req.Content.ReadAsStringAsync();

            dynamic data = JsonConvert.DeserializeObject(jsonContent);

            if (data == null || data.code == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "missing <code> property."
                }));
            }

            // Evaluate CSharp Code
            string code = data.code;

            log.Info($"{nameof(code)} : {code}");
            var resultText = await RoslynCompiler.EvaluateCSharpAsync(code);

            log.Info(resultText);

            return(req.CreateResponse(HttpStatusCode.OK, new
            {
                body = resultText,
            }));
        }
예제 #2
0
        public static async Task <HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
        {
            log.Info("Charp Compiler service Webhook was triggered!");

            string jsonContent = await req.Content.ReadAsStringAsync();

            dynamic data = JsonConvert.DeserializeObject(jsonContent);

            if (data.code == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "missing <code> property."
                }));
            }

            // Evaluate CSharp Code
            string code = data.code;

            log.Info($"{nameof(code)} : {code}");
            var resultText = await RoslynCompiler.EvaluateCSharpAsync(code);

            log.Info(resultText);

            return(req.CreateResponse(HttpStatusCode.OK, new
            {
                body = resultText,
            }));
        }
예제 #3
0
        public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info($"{nameof(CSharpCompilerSlackOuthookCSharp)} : Webhook was triggered!");

            string content = await req.Content.ReadAsStringAsync();

            log.Info(content);

            var data = content
                       .Split('&')
                       .Select(x => x.Split('='))
                       .ToDictionary(x => x[0], x => WebUtility.HtmlDecode(WebUtility.UrlDecode(x[1])));

            if (data["user_name"] == "slackbot")
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    body = "Cannot Support Messages From SlackBot.",
                }));
            }

            var text = data["text"] as string ?? "";

            log.Info(text);

            var code = text.Replace(TRIGGER_WORD, "");

            // Evaluate C# Code with Roslyn
            log.Info($"{nameof(code)} : {code}");
            var resultText = await RoslynCompiler.EvaluateCSharpAsync(code);

            log.Info(resultText);

            // Send back with Slack Incoming Webhook
            var message = string.IsNullOrWhiteSpace(resultText) ? "‹ó‚¾ƒjƒƒ" : resultText;
            var payload = new
            {
                channel  = "#azurefunctions",
                username = "******",
                text     = message,
                icon_url = "https://azure.microsoft.com/svghandler/visual-studio-team-services/?width=300&height=300",
            };

            var jsonString = JsonConvert.SerializeObject(payload);

            using (var client = new HttpClient())
            {
                var res = await client.PostAsync(_slackWebhookUrl, new StringContent(jsonString, Encoding.UTF8, "application/json"));

                return(req.CreateResponse(res.StatusCode, new
                {
                    body = $"CSharp Evaluate message. Message : {message}",
                }));
            }
        }