/// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            GetMetricWidgetImageResponse response = new GetMetricWidgetImageResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.IsStartElement)
                {
                    if (context.TestExpression("GetMetricWidgetImageResult", 2))
                    {
                        UnmarshallResult(context, response);
                        continue;
                    }

                    if (context.TestExpression("ResponseMetadata", 2))
                    {
                        response.ResponseMetadata = ResponseMetadataUnmarshaller.Instance.Unmarshall(context);
                    }
                }
            }

            return(response);
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, GetMetricWidgetImageResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.ReadAtDepth(originalDepth))
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("MetricWidgetImage", targetDepth))
                    {
                        var unmarshaller = MemoryStreamUnmarshaller.Instance;
                        response.MetricWidgetImage = unmarshaller.Unmarshall(context);
                        continue;
                    }
                }
            }

            return;
        }
Ejemplo n.º 3
0
    protected override async Task <bool> HandleMessage(DiscordMessage discordMessage)
    {
        GetMetricWidgetImageRequest request = new GetMetricWidgetImageRequest();

        request.OutputFormat = "png";
        request.MetricWidget = @"
            {
                ""metrics"": [
                    [ ""CutieBot"", ""Discord.SentimentScore.Positive"", { ""label"": ""[avg: ${AVG}] Discord.SentimentScore.Positive"", ""color"": ""#2ca02c"" } ],
                    [ ""."", ""Discord.SentimentScore.Neutral"", { ""label"": ""[avg: ${AVG}] Discord.SentimentScore.Neutral"", ""color"": ""#f4f116"" } ],
                    [ ""."", ""Discord.SentimentScore.Negative"", { ""label"": ""[avg: ${AVG}] Discord.SentimentScore.Negative"", ""color"": ""#d62728"" } ],
                    [ ""."", ""Discord.SentimentScore.Mixed"", { ""label"": ""[avg: ${AVG}] Discord.SentimentScore.Mixed"", ""color"": ""#1f77b4"" } ]
                ],
                ""view"": ""pie"",
                ""stacked"": false,
                ""stat"": ""Average"",
                ""period"": 300,
                ""setPeriodToTimeRange"": true,
                ""width"": 500,
                ""height"": 500,
                ""start"": ""-P1D"",
                ""end"": ""P0D""
            }
        ";
        GetMetricWidgetImageResponse getMetricWidgetImageResponse = await CloudWatchClient.GetMetricWidgetImageAsync(request);

        if (getMetricWidgetImageResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
        {
            return(false);
        }

        await discordMessage.Channel.SendFileAsync(
            content : $"Here's the sentiment of the {discordMessage.Channel.Name} channel",
            file_data : getMetricWidgetImageResponse.MetricWidgetImage,
            file_name : "MetricWidget.png"
            );

        return(true);
    }