c# Bot Framework client and Server timezone difference

不想你离开。 提交于 2019-12-13 03:29:00

问题


I create a chatbot using MS bot framework where initially i will send a welcome message like "Good morning UserName" but my bot is hosted in azure and i user DateTime.Now to find the time of the day and provide the greeting message accordingly. But if some user from different timezone uses "Good morning " and "Good afternoon" messages are not appropriate.

How can i overcome this?

if (DateTime.Now.Hour < 12)
{
    await context.PostAsync("Good Morning");
}
else if (DateTime.Now.Hour > 12 and DateTime.Now.Hour < 17)
{
    await context.PostAsync("Good Afternoon");
}
else
{
    await context.PostAsync("Good Evening");
}

回答1:


If you embed webchat in your website, as Matt mentioned in comment, you can use the backchannel mechanism to pass TimezoneOffset from JavaScript client to your bot application to achieve your requirement.

In JavaScript client:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
    <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
    <style>
        .wc-chatview-panel {
            width: 350px;
            height: 500px;
            position: relative;
        }
    </style>
</head>
<body>
    <div id="bot" />
</body>
</html>
<script>
    var botConnection = new BotChat.DirectLine({ secret: "{directline_secret}" }); 

    var d = new Date();
    var tzoffset = d.getTimezoneOffset();

    BotChat.App({
        botConnection: botConnection,
        user: { id: 'userid' },
        bot: { id: 'fehanbotdg' },
        resize: 'detect'
    }, document.getElementById("bot"));


    botConnection.postActivity({
        type: 'event',
        from: { id: 'userid'},
        name: 'ClientTimezoneOffsetEvent',
        value: tzoffset.toString()
    }).subscribe(function (id) { console.log('ClientTimezoneOffset: "' + tzoffset + '" sent'); });
</script>

In bot application MessagesController:

private Activity HandleSystemMessage(Activity message)
{
    if (message.Type == ActivityTypes.DeleteUserData)
    {
        // Implement user deletion here
        // If we handle user deletion, return a real message
    }
//......
//code logic for other messages types
    //......
    else if (message.Type == ActivityTypes.Event && message.Name == "ClientTimezoneOffsetEvent") {

        int timezoneOffset = Convert.ToInt32(message.Value);

        var client = new ConnectorClient(new Uri(message.ServiceUrl), new MicrosoftAppCredentials());

        timezoneOffset = Convert.ToInt32(message.Value);

        DateTime newDate = DateTime.UtcNow - new TimeSpan(timezoneOffset / 60, timezoneOffset % 60, 0);

        var greeting = "";

        if (newDate.Hour < 12)
        {
            greeting = "Good Morning";
        }
        else if (newDate.Hour > 12 & newDate.Hour <= 17)
        {
            greeting = "Good Afternoon";
        }
        else if (newDate.Hour > 17 & newDate.Hour <= 24)
        {
            greeting = "Good Evening";
        }

        var reply = message.CreateReply();

        reply.Text = $"{greeting}! UTC time: {DateTime.UtcNow}; Client time: {newDate}";

        client.Conversations.ReplyToActivityAsync(reply);
    }

    return null;
}

Test result:



来源:https://stackoverflow.com/questions/50396878/c-sharp-bot-framework-client-and-server-timezone-difference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!