Example #1
0
        public static string CreateUcwaApps(HttpClient httpClient, OAuthTokenRoot authToken, string ucwaApplicationsRootUri,
                                            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 =
                    httpClient.PostAsync(ucwaApplicationsRootUri, new StringContent(createUcwaPostData, Encoding.UTF8,
                                                                                    "application/json")).Result;
                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)
                    {
                        ConfigData.ucwaApplication = obj._links.self.href;
                        // ConfigData.ucwaApplications += ConfigData.ucwaApplication;
                        ConfigData.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);
            }
        }
        static void Main(string[] args)
        {
            string commandString = string.Empty;

            Console.ForegroundColor = ConsoleColor.White;

            //Configure the app insights instrumentation key
            string instrumentationKey = ConfigurationManager.AppSettings["AppInsightsIKey"];

            TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;
            TelemetryClient tc = new TelemetryClient();

            try
            {
                //Set the UCWA applications host
                ConfigData.ucwaApplicationsHost = ucwaApplicationsHost;

                //Get the Authentication token
                ConfigData.authToken = LyncAuth.GetOAuthToken(httpClient, userName, password, domain, lyncOAuthUri, tc);
                if (ConfigData.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.
                    createUcwaAppsResults = UcwaApplications.CreateUcwaApps(httpClient, ConfigData.authToken, ucwaApplicationsUri, ucwaMyAppsObject, tc);

                    //Set IM status as online
                    SetIMStatus(tc);

                    //Get the message. The method "GetIM_Step03_Events" in the class UCWAReceiveMessage is a recursive function
                    //which will keep listening for the incoming messages
                    GetMessage(tc, true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                tc.TrackException(ex);
            }

            while (!commandString.Equals("Exit", StringComparison.InvariantCultureIgnoreCase))
            {
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine("Enter exit to exit");
                commandString = Console.ReadLine();
                //commandString = "msg";
                switch (commandString.ToUpper())
                {
                case "EXIT":
                    Console.WriteLine("Bye!");;
                    break;

                default:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Invalid command.");
                    break;
                }
            }
        }