Exemplo n.º 1
0
        /// <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);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <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);
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <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);
                    }
                }
            }
        }