c#Bot Framework客户端和服务器时区差异 [英] c# Bot Framework client and Server timezone difference

查看:81
本文介绍了c#Bot Framework客户端和服务器时区差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MS Bot框架创建了一个聊天机器人,最初我会发送一个欢迎消息,例如"Good morning UserName",但我的Bot托管在azure中,并且我使用DateTime.现在查找一天中的时间并提供问候消息因此.但是,如果来自不同时区的某些用户使用早安"和下午好"消息是不合适的.

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.

我该如何克服呢?

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");
}

推荐答案

如果您将Webchat嵌入网站中(如Matt在评论中提到的那样),则可以使用

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.

在JavaScript客户端中:

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>

在漫游器应用程序MessagesController中:

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:

这篇关于c#Bot Framework客户端和服务器时区差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆