예제 #1
0
        } // Get()

        public ActionResult <string> Post([FromQuery] string validationToken = null)
        {
            // handle validation
            if (!string.IsNullOrEmpty(validationToken))
            {
                //Console.WriteLine($"Received Token: '{validationToken}'");
                //_logger.LogWarning($"Received Token: '{validationToken}'");
                this.telemetryClient.TrackEvent("Validation", new Dictionary <string, string> {
                    { "ValidationToken", $"{validationToken}" }
                });
                return(Ok(validationToken));
            }

            // handle notifications
            using (StreamReader reader = new StreamReader(Request.Body))
            {
                Task <string> content = reader.ReadToEndAsync();

                //Console.WriteLine(content.GetAwaiter().GetResult());

                var notifications = JsonConvert.DeserializeObject <Notifications>(content.GetAwaiter().GetResult());
                //Console.WriteLine(content.GetAwaiter().GetResult());

                if (this.graphClient == null)
                {
                    this.graphClient = GraphHelper.Instance.GetGraphClient();
                }
                var accessToken = GraphHelper.Instance.GetAccessToken().Result;

                foreach (var notification in notifications.Items)
                {
                    _logger.LogWarning($"Received notification: '{notification.Resource}', {notification.ResourceData?.Id}");
                    this.telemetryClient.TrackEvent("Received notification", new Dictionary <string, string> {
                        { $"{notification.Resource}", $"{notification.ResourceData?.Id}" }
                    });
                    // Calls abrufen
                    string callRecordStr = notification.ResourceData?.Id.ToString();

                    var client = new HttpClient();
                    client.DefaultRequestHeaders.Authorization =
                        new AuthenticationHeaderValue("Bearer", accessToken);
                    var con = client.GetAsync($"https://graph.microsoft.com/v1.0/communications/callrecords/{callRecordStr}").Result;

                    var jsonString = con.Content.ReadAsStringAsync().Result;

                    // Update CosmosDB with notifications
                    try
                    {
                        cosmos.AddItemToContainerAsync(jsonString).GetAwaiter().GetResult();
                    }
                    catch (Exception e)
                    {
                        var dict = new Dictionary <string, string> {
                            { $"message", $"{e.Message}" }, { $"Stacktrace", $"{e.StackTrace}" }
                        };
                        telemetryClient.TrackEvent("EXCEPTION THROWN", dict);
                    }
                } // foreach
            }
            // use deltaquery to query for all updates
            CheckForUpdates();
            return(Ok());
        } // Post([FromQuery]string validationToken = null)