/// <summary> /// Entry point. /// </summary> /// <param name="args">args of this app.</param> /// <returns>Task for async.</returns> static async Task MainAsync(string[] args) { /* ******************************************************** * NOTE: THIS IS ONLY A SAMPLE. * I will put most codes in this Main() on purpose. * So, you will be able to understand the sample * after you read it from top to bottom. * You do not need to care about 'SampleUtil' in this code. * The 'SampleUtil' does something tedious. * Only you need to do to understand the sample * is reading this code in Main() from top to bottom. * *********************************************************/ SampleUtil.ShowTitle("[S0010] Setup for samples", "Encrypt Bot token and find or create a space for the samples."); ////////////////////////////////// // Read bot token from you. Console.WriteLine("Please copy and paste Bot token you want to use in the samples."); Console.WriteLine("And then, press enter key."); Console.Write("Bot token here> "); string token = Console.ReadLine(); if (!String.IsNullOrEmpty(token)) { ////////////////////////////////// // Encrypt the token. var encryptedToken = LocalProtectedString.FromString(token); ////////////////////////////////// // Check if the token is for bot. var teams = TeamsAPI.CreateVersion1Client(encryptedToken); var rMe = await teams.GetMeFromCacheAsync(); if (rMe.IsSuccessStatus) { var me = rMe.Data; Console.WriteLine("-------"); Console.WriteLine("Name: {0}", me.DisplayName); Console.WriteLine("Type: {0}", me.TypeName); Console.WriteLine("-------"); if (me.Type != PersonType.Bot) { Console.WriteLine("The person is not Bot."); Console.WriteLine("For most samples, the Bot account is strongly recommended."); if (!SampleUtil.WaitKeyPress("Press 'y' if you want to proceed with 'Non'-bot account. Press other key to cancel.", 'y')) { SampleUtil.ShowMessage("Setup canceled."); return; } } ////////////////////////////////// // Export encrypted token data. var dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)); string path = dir.CreateSubdirectory(".thrzn41").CreateSubdirectory("WebexTeamsAPIClientSamples").CreateSubdirectory("V1Samples").FullName; using (var fs = new FileStream(String.Format("{0}{1}token.dat", path, Path.DirectorySeparatorChar), FileMode.Create, FileAccess.Write, FileShare.Read)) { await fs.WriteAsync(encryptedToken.EncryptedData, 0, encryptedToken.EncryptedData.Length); } using (var fs = new FileStream(String.Format("{0}{1}entropy.dat", path, Path.DirectorySeparatorChar), FileMode.Create, FileAccess.Write, FileShare.Read)) { await fs.WriteAsync(encryptedToken.Entropy, 0, encryptedToken.Entropy.Length); } Console.WriteLine("Encrypted token was exported: Path = {0}", path); ////////////////////////////////////////////////////////// // Read Webex Teams account to add to the sample space. Console.WriteLine(); Console.WriteLine("This app will create or find the space for the sample, and then add your Webex Teams account to the space."); Console.WriteLine("Please enter your Webex Teams account(email address style)."); Console.Write("Enter Webex Teams account> "); string teamsAccount = Console.ReadLine(); if (!String.IsNullOrEmpty(teamsAccount)) { Console.WriteLine(); Console.WriteLine("Please confirm the Webex Teams account carefully."); Console.WriteLine("This account will be added to the space for the sample."); Console.WriteLine(); Console.WriteLine("Entered account: {0}", teamsAccount); if (!SampleUtil.WaitKeyPress("Press 'y' if your Webex Teams account is correct. Press other key to cancel.", 'y')) { SampleUtil.ShowMessage("Setup canceled."); return; } ////////////////////////////////// // Try to find the sample space. Space spaceForSample = null; var e = (await teams.ListSpacesAsync()).GetListResultEnumerator(); while (await e.MoveNextAsync()) { var rSpaces = e.CurrentResult; if (rSpaces.IsSuccessStatus && rSpaces.Data.HasItems) { var spaces = rSpaces.Data; foreach (var item in spaces.Items) { if (item.Title.EndsWith("#WebexTeamsAPIClientV1SamplesSpace")) { spaceForSample = item; break; } } } if (spaceForSample != null) { break; } } ////////////////////////////////////////////////////////// // Create a new sample space when no sample space found. if (spaceForSample == null) { var rSpace = await teams.CreateSpaceAsync("Webex Teams API Client Samples #WebexTeamsAPIClientV1SamplesSpace"); if (rSpace.IsSuccessStatus) { spaceForSample = rSpace.Data; } } if (spaceForSample != null) { //////////////////////////////////////////////////////// // Try to add Webex Teams account to the sample space. var rSpaceMembership = await teams.CreateSpaceMembershipAsync(spaceForSample, teamsAccount); if (rSpaceMembership.IsSuccessStatus || rSpaceMembership.HttpStatusCode == System.Net.HttpStatusCode.Conflict) { SampleUtil.ShowMessage("{0} was added to Space for the samples", teamsAccount); } else { SampleUtil.ShowMessage("Failed to add {0} to Space for the samples: Error = {1}", teamsAccount, rSpaceMembership.HttpStatusCode); } Console.WriteLine("-------"); Console.WriteLine("Space: {0}", spaceForSample.Title); Console.WriteLine("-------"); } else { SampleUtil.ShowMessage("Failed to find or create Space for the samples."); return; } SampleUtil.ShowMessage("Setup for the samples has completed."); } else { SampleUtil.ShowMessage("Webex Teams account is null or empty."); return; } } else { SampleUtil.ShowMessage("Failed to get person info from Webex Teams API service: Error = {0}", rMe.HttpStatusCode); return; } } else { SampleUtil.ShowMessage("Token is null or empty."); return; } }
/// <summary> /// Entry point. /// </summary> /// <param name="args">args of this app.</param> /// <returns>Task for async.</returns> static async Task MainAsync(string[] args) { /* ******************************************************** * NOTE: THIS IS ONLY A SAMPLE. * I will put most codes in this Main() on purpose. * So, you will be able to understand the sample * after you read it from top to bottom. * You do not need to care about 'SampleUtil' in this code. * The 'SampleUtil' does something tedious. * Only you need to do to understand the sample * is reading this code in Main() from top to bottom. * *********************************************************/ SampleUtil.ShowTitle("[S1030] Markdown Builder", "How to use markdown builder."); // Load encrypted token that is encrypted by 'S0010SetupSamples'. ProtectedString token = SampleUtil.LoadEncryptedToken(); if (token != null) { //////////////////////////////////////////////////////////////////////////// // Create an instance for Webex Teams API. // As best practice, the instance should be re-used as long as possible. // For bots, the lifetime of the instance typically is almost the same as the lifetime of the app process. var teams = TeamsAPI.CreateVersion1Client(token); // Try to find Sample space. var space = await SampleUtil.FindSampleSpaceAsync(teams); if (space != null) { //////////////////////////////////////////////////////////////// // You can build markdown that can be used in Webex Teams API. var md = new MarkdownBuilder(); // Bold md.Append("Hello, Bold ").AppendBold("WebexTeams").Append("!!").AppendLine(); // Italic md.Append("Hello, Italic ").AppendItalic("WebexTeams").Append("!!").AppendLine(); // Link md.AppendParagraphSeparater(); md.AppendBlockQuote("Hi!").AppendLink("This is Link", new Uri("https://www.google.com/")).Append("."); // Block Quote md.AppendParagraphSeparater(); md.AppendBlockQuote("Hi! This is Block Quote."); // Ordered List md.AppendParagraphSeparater(); md.AppendBold("This is Ordered list:").AppendLine(); md.AppendOrderedList("list item 01"); md.AppendOrderedList("list item 02"); md.AppendOrderedList("list item 03"); // Unordered List md.AppendParagraphSeparater(); md.AppendBold("This is Unordered list:").AppendLine(); md.AppendUnorderedList("list item 01"); md.AppendUnorderedList("list item 02"); md.AppendUnorderedList("list item 03"); // Inline Code. md.AppendParagraphSeparater(); md.Append("The ").AppendInLineCode("print(\"Hello, World!!\")").Append(" is inline code."); // Code Block. md.AppendParagraphSeparater(); md.Append("This is Code Block:").AppendLine(); md.BeginCodeBlock() .Append("#include <stdio.h>\n") .Append("\n") .Append("int main(void)\n") .Append("{\n") .Append(" printf(\"Hello, World!!\\n\");\n") .Append("\n") .Append(" return 0;\n") .Append("}\n") .EndCodeBlock(); var r = await teams.CreateMessageAsync(space, md.ToString()); if (r.IsSuccessStatus) { SampleUtil.ShowMessage("Succeeded to post a message: Id = {0}", r.Data.Id); } else { SampleUtil.ShowMessage("Failed to post a message: StatusCode = {0}", r.HttpStatusCode); } // Mentioned to All. md.Clear(); md.Append("Hi ").AppendMentionToAll().Append(", this message is mentioned to all in the space."); r = await teams.CreateMessageAsync(space, md.ToString()); if (r.IsSuccessStatus) { SampleUtil.ShowMessage("Succeeded to post a message: Id = {0}", r.Data.Id); } else { SampleUtil.ShowMessage("Failed to post a message: StatusCode = {0}", r.HttpStatusCode); } } } }
/// <summary> /// Entry point. /// </summary> /// <param name="args">args of this app.</param> /// <returns>Task for async.</returns> static async Task MainAsync(string[] args) { /* ******************************************************** * NOTE: THIS IS ONLY A SAMPLE. * I will put most codes in this Main() on purpose. * So, you will be able to understand the sample * after you read it from top to bottom. * You do not need to care about 'SampleUtil' in this code. * The 'SampleUtil' does something tedious. * Only you need to do to understand the sample * is reading this code in Main() from top to bottom. * *********************************************************/ SampleUtil.ShowTitle("[S1040] ListResult Enumerator(Pagination)", "Get first list result, and then get next list result..."); // Load encrypted token that is encrypted by 'S0010SetupSamples'. ProtectedString token = SampleUtil.LoadEncryptedToken(); if (token != null) { //////////////////////////////////////////////////////////////////////////// // Create an instance for Webex Teams API. // As best practice, the instance should be re-used as long as possible. // For bots, the lifetime of the instance typically is almost the same as the lifetime of the app process. var teams = TeamsAPI.CreateVersion1Client(token); ///////////////////////////////////////////////////// // Create 4 temporary spaces for the sample. for (int i = 0; i < 4; i++) { string title = String.Format("Sample Temporary Space-{0} #WebexTeamsAPIClientV1SamplesTemporary", i); SampleUtil.ShowMessage("Create a Space: {0}", title); var r = await teams.CreateSpaceAsync(title); if (r.IsSuccessStatus) { SampleUtil.ShowMessage("Succeeded to create space."); } else { SampleUtil.ShowMessage("Failed to create space, StatusCode = {0}", r.HttpStatusCode); } } ///////////////////////////////////////////////////// // List spaces for each 2 spaces. // In this case, 'max: 2' parameter is set. var e = (await teams.ListSpacesAsync( max: 2, type: SpaceType.Group, sortBy: SpaceSortBy.Created) ).GetListResultEnumerator(); // In this sample, give up iteration after getting 4 spaces. int count = 4; // Iterate list result. while (await e.MoveNextAsync()) { // Get current result. var r = e.CurrentResult; if (r.IsSuccessStatus && r.Data.HasItems) { SampleUtil.ShowMessage("Succeeded to get {0} spaces.", r.Data.ItemCount); foreach (var space in r.Data.Items) { SampleUtil.ShowMessage(" Title: {0}", space.Title); count--; } } if (count <= 0) { break; } } ///////////////////////////////////////////////////// // Clean up the temporary created spaces. SampleUtil.ShowMessage("Cleaning up the temporary created spaces."); var temporarySpaces = new List <Space>(); ///////////////////////////////////////////////////// // Try to find all the temporary created spaces. e = (await teams.ListSpacesAsync(type: SpaceType.Group)).GetListResultEnumerator(); while (await e.MoveNextAsync()) { var r = e.CurrentResult; if (r.IsSuccessStatus && r.Data.HasItems) { foreach (var space in r.Data.Items) { if (space.Title.Contains("#WebexTeamsAPIClientV1SamplesTemporary")) { temporarySpaces.Add(space); } } } } ///////////////////////////////////////////////////// // Delete the temporary created spaces found. foreach (var space in temporarySpaces) { SampleUtil.ShowMessage("Deleting {0}", space.Title); var r = await teams.DeleteSpaceAsync(space); if (r.IsSuccessStatus) { SampleUtil.ShowMessage("Succeeded to delete space."); } else { SampleUtil.ShowMessage("Failed to delete space, StatusCode = {0}", r.HttpStatusCode); } } } }
/// <summary> /// Entry point. /// </summary> /// <param name="args">args of this app.</param> /// <returns>Task for async.</returns> static async Task MainAsync(string[] args) { /* ******************************************************** * NOTE: THIS IS ONLY A SAMPLE. * I will put most codes in this Main() on purpose. * So, you will be able to understand the sample * after you read it from top to bottom. * You do not need to care about 'SampleUtil' in this code. * The 'SampleUtil' does something tedious. * Only you need to do to understand the sample * is reading this code in Main() from top to bottom. * *********************************************************/ SampleUtil.ShowTitle("[S1020] Check if a request suceeded or not", "Sample to check if a request suceeded or not, or handle TeamsResultException."); // Load encrypted token that is encrypted by 'S0010SetupSamples'. ProtectedString token = SampleUtil.LoadEncryptedToken(); if (token != null) { //////////////////////////////////////////////////////////////////////////// // Create an instance for Webex Teams API. // As best practice, the instance should be re-used as long as possible. // For bots, the lifetime of the instance typically is almost the same as the lifetime of the app process. var teams = TeamsAPI.CreateVersion1Client(token); // Try to find Sample space. var space = await SampleUtil.FindSampleSpaceAsync(teams); if (space != null) { ///////////////////////////////////////////////////////////////////// // result.IsSuccessStatus indicates the request succeeded or not. var r = await teams.CreateMessageAsync(space, "This message will be posted."); if (r.IsSuccessStatus) { SampleUtil.ShowMessage("Succeeded to post a message: Id = {0}", r.Data.Id); } else { SampleUtil.ShowMessage("Failed to post a message: StatusCode = {0}, TrackingId = {1}", r.HttpStatusCode, r.TrackingId); } ///////////////////////////////////////////////////////////////////// // This request will not succeed because the space id is invalid. r = await teams.CreateMessageAsync("invalid_space_id", "Bacause of invalid space id, this message will not be posted."); if (r.IsSuccessStatus) { SampleUtil.ShowMessage("Succeeded to post a message: Id = {0}", r.Data.Id); } else { SampleUtil.ShowMessage("Failed to post a message: StatusCode = {0}, TrackingId = {1}", r.HttpStatusCode, r.TrackingId); } //////////////////////////////////////////////////////////////////////// // result.GetData() throws TeamsResultException on error response. // On the other hands, result.Data does not throw TeamsResultException. try { r = await teams.CreateMessageAsync("invalid_space_id", "Bacause of invalid space id, this message will not be posted."); ///////////////////////////////////////////////////// // This does not throw TeamsResultException. // And empty id will be shown. var message = r.Data; SampleUtil.ShowMessage("Message id by r.Data.Id = {0}", message.Id); ///////////////////////////////////////////////////// // This throws TeamsResultException. // So, the id will not be shown. message = r.GetData(); SampleUtil.ShowMessage("Message id by r.GetData().Id = {0}", message.Id); } catch (TeamsResultException tre) { SampleUtil.ShowMessage("{0}: StatusCode = {1}, TrackingId = {2}", tre.Message, tre.HttpStatusCode, tre.TrackingId); } } } }
/// <summary> /// Entry point. /// </summary> /// <param name="args">args of this app.</param> /// <returns>Task for async.</returns> static async Task MainAsync(string[] args) { /* ******************************************************** * NOTE: THIS IS ONLY A SAMPLE. * I will put most codes in this Main() on purpose. * So, you will be able to understand the sample * after you read it from top to bottom. * You do not need to care about 'SampleUtil' in this code. * The 'SampleUtil' does something tedious. * Only you need to do to understand the sample * is reading this code in Main() from top to bottom. * *********************************************************/ SampleUtil.ShowTitle("[S1010] Post a message", "Post a message to Sample space."); // Load encrypted token that is encrypted by 'S0010SetupSamples'. ProtectedString token = SampleUtil.LoadEncryptedToken(); if (token != null) { //////////////////////////////////////////////////////////////////////////// // Create an instance for Webex Teams API. // As best practice, the instance should be re-used as long as possible. // For bots, the lifetime of the instance typically is almost the same as the lifetime of the app process. var teams = TeamsAPI.CreateVersion1Client(token); // Try to find Sample space. var space = await SampleUtil.FindSampleSpaceAsync(teams); if (space != null) { ///////////////////////////////////////////////////////////////// // By default, the API Client posts a message as markdown. var r = await teams.CreateMessageAsync(space, "Hello, **Webex Teams**!!"); if (r.IsSuccessStatus) { SampleUtil.ShowMessage("Succeeded to post a message: Id = {0}", r.Data.Id); } else { SampleUtil.ShowMessage("Failed to post a message: StatusCode = {0}", r.HttpStatusCode); } ///////////////////////////////////////////////////////////////// // To post a message as normal text, use 'textType: MessageTextType.Text' parameter. r = await teams.CreateMessageAsync(space, "Hello, **Webex Teams**!!", textType : MessageTextType.Text); if (r.IsSuccessStatus) { SampleUtil.ShowMessage("Succeeded to post a message: Id = {0}", r.Data.Id); } else { SampleUtil.ShowMessage("Failed to post a message: StatusCode = {0}", r.HttpStatusCode); } } } }