// Constructor dependency injection
 public UCWAApplication(HttpClient httpClient, OAuthTokenRoot authToken, string ucwaApplicationsRootUrl,
                        UCWAMyApps ucwaAppsObject, TelemetryClient tc)
 {
     UCWAConfiguration._httpClient = httpClient;
     _ucwaApplicationsRootUrl      = ucwaApplicationsRootUrl;
     _authToken            = authToken;
     _ucwaAppsObject       = ucwaAppsObject;
     UCWAConfiguration._tc = tc;
 }
        public async Task <string> CreateUcwaApps(HttpClient httpClient, OAuthTokenRoot authToken, string ucwaApplicationsRootUrl,
                                                  UCWAMyApps ucwaAppsObject, TelemetryClient tc)
        {
            try
            {
                string createUcwaAppsResults = string.Empty;

                httpClient.DefaultRequestHeaders.Clear();
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken.access_token);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var createUcwaPostData  = JsonConvert.SerializeObject(ucwaAppsObject);
                var httpResponseMessage = await httpClient.PostAsync(ucwaApplicationsRootUrl, new StringContent(createUcwaPostData,
                                                                                                                Encoding.UTF8, "application/json"));

                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    tc.TrackTrace("Application on the UCWA server created successfully.");
                    Console.WriteLine("Application on the UCWA server created successfully.");
                    createUcwaAppsResults = httpResponseMessage.Content.ReadAsStringAsync().Result;
                    ApplicationRoot obj = new ApplicationRoot();
                    JsonConvert.PopulateObject(createUcwaAppsResults, obj);
                    if (obj != null)
                    {
                        UCWAConfiguration.ucwaApplication = obj._links.self.href;
                        // ConfigData.ucwaApplications += ConfigData.ucwaApplication;
                        UCWAConfiguration.ucwaEvents = obj._links.events.href;
                    }
                }
                else
                {
                    tc.TrackTrace("Failed to create application on the UCWA server.");
                    tc.TrackEvent("CreateUcwaApps-Failed");
                    Console.WriteLine("Failed to create application on the UCWA server.");
                }
                return(createUcwaAppsResults);
            }
            catch (Exception ex)
            {
                tc.TrackException(ex);
                tc.TrackEvent("CreateUcwaApps-Exception");
                Console.WriteLine("Failed to create application on the UCWA server.");
                throw new CustomException("Error occured in " + MethodBase.GetCurrentMethod().Name + ":" + ex.Message +
                                          " TargetSite:" + ex.TargetSite + " StackTrace: " + ex.StackTrace);
            }
        }
Exemplo n.º 3
0
        static async Task Main(string[] args)
        {
            string commandString = string.Empty;

            Console.ForegroundColor = ConsoleColor.White;

            try
            {
                // Register the dependencies
                DI.RegisterServices();

                // Load the configuration settings file
                DI.LoadConfiguration();

                //Configure the app insights instrumentation key
                TelemetryConfiguration.Active.InstrumentationKey = DI._config["appinsights_key"];

                // Get the authentication token from Lync Server
                var lyncAuthenticationService = DI._serviceProvider.GetService <ILyncAuthentication>();
                UCWAConfiguration.authToken = await lyncAuthenticationService.GetOAuthToken(UCWAConfiguration._httpClient, DI._config.GetSection("Skypebot:user_name").Value,
                                                                                            DI._config.GetSection("Skypebot:password").Value, DI._config.GetSection("Skypebot:domain").Value, DI._config.GetSection("UCWA:lync_oauth_url").Value, UCWAConfiguration._tc);

                //Since the auth token expires in around 7.5 hours, get a new token only 100 seconds before it expires.
                //This call is made in succession here because "ConfigData.authToken.expires_in" value is fetched only
                //after getting the token for the first time. So initially the authentication calls are back to back.
                var tokenExpirationTimer = new System.Threading.Timer(
                    async e => await lyncAuthenticationService.GetOAuthToken(UCWAConfiguration._httpClient, DI._config.GetSection("Skypebot:user_name").Value,
                                                                             DI._config.GetSection("Skypebot:password").Value, DI._config.GetSection("Skypebot:domain").Value, DI._config.GetSection("UCWA:lync_oauth_url").Value, UCWAConfiguration._tc),
                    null,
                    TimeSpan.Zero,
                    TimeSpan.FromSeconds(UCWAConfiguration.authToken.expires_in - 100));

                if (UCWAConfiguration.authToken.access_token != null)
                {
                    //Create an application on the UCWA server
                    List <string> Modalities = new List <string>();
                    Modalities.Add("PhoneAudio");
                    Modalities.Add("Messaging");
                    //Set the properties for the applications resource
                    UCWAMyApps ucwaMyAppsObject = new UCWAMyApps()
                    {
                        UserAgent  = "IntranetBot",
                        EndpointId = Guid.NewGuid().ToString(),
                        Culture    = "en-US"
                    };
                    //Create an application on the Skype UCWA server and register it.
                    var ucwaApplicationService = DI._serviceProvider.GetService <IUCWAApplication>();
                    UCWAConfiguration.createUcwaAppsResults = await ucwaApplicationService.CreateUcwaApps(UCWAConfiguration._httpClient,
                                                                                                          UCWAConfiguration.authToken, DI._config.GetSection("UCWA:ucwa_applications_url").Value, ucwaMyAppsObject, UCWAConfiguration._tc);

                    //Set IM status as online
                    var manageIMStatusService = DI._serviceProvider.GetService <IManageIMStatus>();
                    await manageIMStatusService.SetIMStatus(UCWAConfiguration._httpClient, UCWAConfiguration._tc);

                    //Keep the bot Online all the time else it will show away after 5 minutes
                    var timer = new System.Threading.Timer(
                        async e => await manageIMStatusService.KeepStatusOnline(UCWAConfiguration._httpClient, UCWAConfiguration._tc),
                        null,
                        TimeSpan.Zero,
                        TimeSpan.FromMinutes(3));

                    //Get the message. The method "GetIM_Step03_Events" in the class UCWAReceiveMessage is a recursive function
                    //which will keep listening for the incoming messages
                    await GetMessage(UCWAConfiguration._tc, true);
                }

                // Calling readline to avoid calling the dispose services
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                UCWAConfiguration._tc.TrackException(ex);
                UCWAConfiguration._tc.TrackEvent("MainFunction-Exception");
            }
            finally
            {
                // Calling Dispose() on service provider is mandatory as otherwise registered instances will not get disposed.
                DI.DisposeServices();
            }
        }
        public async Task KeepMeOnline(HttpClient httpClient, OAuthTokenRoot authToken, String ucwaKeepMeOnlineUrl,
                                       TelemetryClient tc)
        {
            try
            {
                httpClient.DefaultRequestHeaders.Clear();
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken.access_token);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //Successfull response is Http 204 No content
                var httpResponseMessage = await
                                          httpClient.PostAsync(ucwaKeepMeOnlineUrl, new StringContent(string.Empty, Encoding.UTF8,
                                                                                                      "application/json"));

                string result = httpResponseMessage.Content.ReadAsStringAsync().Result;
                if (result != String.Empty)
                {
                    //Trace the response message if reportMyActivity fails. Also send the response message why it failed.
                    tc.TrackTrace("Failed to set the bot status active. " + httpResponseMessage.ToString());
                    //Retry 3 times by calling the method recursively with an interval of 20 seconds
                    if (counter < 3)
                    {
                        //If the status code is 401, get the token again
                        if (httpResponseMessage.ToString().Contains("StatusCode: 401"))
                        {
                            tc.TrackEvent("Retry-401");
                            //Send email reporting this error
                            // Generic GetService< T> method is an extension method. Add namespace Microsoft.Extensions.DependencyInjection
                            var notificationService = DI._serviceProvider.GetService <INotification>();
                            await notificationService.SendMail("Bot Error 401", httpResponseMessage.ToString(), tc);

                            // Get the authentication token from Lync Server.
                            // Generic GetService< T> method is an extension method. Add namespace Microsoft.Extensions.DependencyInjection
                            var lyncAuthenticationService = DI._serviceProvider.GetService <ILyncAuthentication>();
                            UCWAConfiguration.authToken = await lyncAuthenticationService.GetOAuthToken(UCWAConfiguration._httpClient, DI._config.GetSection("Skypebot:user_name").Value,
                                                                                                        DI._config.GetSection("Skypebot:password").Value, DI._config.GetSection("Skypebot:domain").Value, DI._config.GetSection("UCWA:lync_oauth_url").Value, UCWAConfiguration._tc);

                            tc.TrackEvent("BotRecovered-401");
                            // Recursively call itself
                            await KeepMeOnline(httpClient, UCWAConfiguration.authToken, ucwaKeepMeOnlineUrl, tc);
                        }
                        // Address 404 Not found - Added below if statement
                        else if (httpResponseMessage.ToString().Contains("StatusCode: 404"))
                        {
                            tc.TrackEvent("Retry-404");
                            //Delete the exisiting UCWA application. The UCWA application identifier is the GUID (EndpointID),
                            //but to delete the ucwa application, you need the root application url.
                            //The UCWA application url will be of format /ucwa/oauth/v1/applications/103004647364
                            var ucwaApplicationService = DI._serviceProvider.GetService <IUCWAApplication>();
                            await ucwaApplicationService.DeleteUcwaApps(httpClient, UCWAConfiguration.authToken, tc);

                            //Get the Authentication token
                            //LyncAuth.GetOAuthToken(httpClient, ConfigData.userName, ConfigData.password,
                            //    ConfigData.domain, ConfigData.lyncOAuthUri, tc);

                            //Create an application on the UCWA server
                            List <string> Modalities = new List <string>();
                            Modalities.Add("PhoneAudio");
                            Modalities.Add("Messaging");
                            //Set the properties for the applications resource
                            UCWAMyApps ucwaMyAppsObject = new UCWAMyApps()
                            {
                                UserAgent  = "Skypebot",
                                EndpointId = Guid.NewGuid().ToString(),
                                Culture    = "en-US"
                            };
                            //Create an application on the Skype UCWA server and register it.
                            string createUcwaAppsResults;
                            createUcwaAppsResults = await ucwaApplicationService.CreateUcwaApps(UCWAConfiguration._httpClient,
                                                                                                UCWAConfiguration.authToken, DI._config.GetSection("UCWA:ucwa_applications_url").Value, ucwaMyAppsObject, UCWAConfiguration._tc);

                            //Invoke MakeMeAvailable else it will give 409 conflict error i.e. invoking a resource before setting the status "online"
                            //Set IM status as online
                            var manageIMStatusService = DI._serviceProvider.GetService <IManageIMStatus>();
                            await manageIMStatusService.SetIMStatus(UCWAConfiguration._httpClient, UCWAConfiguration._tc);

                            //Start listening to messages
                            var ucwaReceiveMessageService = DI._serviceProvider.GetService <IUCWAReceiveMessage>();
                            await ucwaReceiveMessageService.GetMessage(UCWAConfiguration._httpClient, tc, UCWAConfiguration.authToken, false);

                            Console.WriteLine("Bot recovered. Bot is listening to messages...");
                            tc.TrackEvent("BotRecovered-404");

                            if (counter < 3)
                            {
                                //increment the counter so that it doesn't send infinite emails
                                counter++;
                                //Send email reporting this error
                                // Generic GetService< T> method is an extension method. Add namespace Microsoft.Extensions.DependencyInjection
                                var notificationService = DI._serviceProvider.GetService <INotification>();
                                await notificationService.SendMail("Bot Error 404", httpResponseMessage.ToString(), tc);
                            }
                        }
                        else
                        {
                            Thread.Sleep(20000);
                            counter++;
                            tc.TrackTrace("Retrying to set the bot status active. Attempt # " + counter);
                            tc.TrackEvent("Retry-KeepOnline");
                            // Recursively call itself
                            await KeepMeOnline(httpClient, UCWAConfiguration.authToken, ucwaKeepMeOnlineUrl, tc);
                        }
                    }
                }
                else
                {
                    //set counter back to zero
                    counter = 0;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to set the bot status active." + ex.Message);
                tc.TrackTrace("Failed to set the bot status active. More details can be found in the exception.");
                tc.TrackException(ex);
            }
        }