2

I'm developing a Chat Bot in C# using Azure Bot Framework (v4),Currently I have a setup to display a welcome text which is used to greet the user.

Current Greetings /welcome text:- Hi User, I'm Bot. How can I help you today.

current Code:

protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
    {

        foreach (var member in membersAdded)
        {
           if (member.Id != turnContext.Activity.Recipient.Id)
         {
                await turnContext.SendActivityAsync(MessageFactory.Text($"Hi  **{member.Name**. I am Bot." {welcomeText}), cancellationToken);

           }
        }

    }

Now I want my bot to detect the current login user's timezone and greet him as per timings.

Expected Greetings /welcome text:(when user chats at morning) Hi user, Good Morning .! I'm Bot. How can i help you today.

Expected Greetings /welcome text:(when user chats at evening) Hi user, Good Evening.! I'm Bot. How can i help you today.

I want to make use of QnAmker or the default welcome text used in the bot.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
Jegan Baskaran
  • 337
  • 2
  • 16

2 Answers2

2

Seems you are trying to get user local time and based on that you would like your bot to greet the user. You could try below code snippet:

protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            foreach (var member in membersAdded)
            {
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    DateTime dateTime = DateTime.Now;

                    DateTime utcTime = dateTime.ToUniversalTime();

                    TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");

                    DateTime yourLocalTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, cstZone);


                    if (yourLocalTime.Hour <= 12)
                    {

                        await turnContext.SendActivityAsync(MessageFactory.Text($"Hello {member.Name}, good morning"), cancellationToken);
                    }
                    else if (yourLocalTime.Hour > 12)
                    {

                        await turnContext.SendActivityAsync(MessageFactory.Text($"Hello {member.Name}, good afternoon"), cancellationToken);
                    }
                    else if (yourLocalTime.Hour > 17)
                    {

                        await turnContext.SendActivityAsync(MessageFactory.Text($"Hello {member.Name}, good evening"), cancellationToken);
                    }
                    else
                    {                      
                        await turnContext.SendActivityAsync(MessageFactory.Text($"Hello {member.Name} " + yourLocalTime), cancellationToken);
                    }


                }
            }
        }

Note: As you know each zone has its own time frame so you could refer this official document for further customization. I have shown China Standard Time for example. You could customize as per your requirement.

For further customization you could follow below interface on screen shot. Just explore it by pressing F12 on FindSystemTimeZoneById on above mentioned code.

enter image description here

Hope this would help.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • @Kiron, thanks for the solution , that worked for me, i made the necessary customization and made it worked as per my requirement. – Jegan Baskaran Feb 10 '20 at 10:44
  • Great solution, do you know if the same TimeZoneInfo and convertTime are available in nodejs? – billoverton Feb 10 '20 at 15:21
  • Also I might suggest that you use Switch/Case here instead of if/else, though functionally I don't think it matters. – billoverton Feb 10 '20 at 15:22
  • 1
    @billoverton `switch (yourLocalTime.Hour) { case int time when time <= 12: await turnContext.SendActivityAsync(MessageFactory.Text($"Hello {member.Name}, good morning"), cancellationToken); break; case int time when time > 12: await turnContext.SendActivityAsync(MessageFactory.Text($"Hello {member.Name}, good afternoon"), cancellationToken); break; }` – Md Farid Uddin Kiron Feb 10 '20 at 16:10
  • Additionally, I am not sure about node.js library. – Md Farid Uddin Kiron Feb 10 '20 at 16:11
0

This information is Channel specific. It is passed in the Activity ChannelData property

Pavel Shastov
  • 2,657
  • 1
  • 18
  • 26