从Azure部署的bot向MS团队发送主动消息

问题描述:

我已经将Bot部署到Azure,当以Azure方式连接到ms团队频道时,我能够ping Bot并接收消息,这很好. 我还在该漫游器中添加了主动消息传递,该消息将在频道中每隔一分钟触发一次消息.

I've already deployed the bot to azure, when connecting to the ms team channel in azure, i'm able to ping the bot and receive messages which is good. I've also added a proactive messaging in the bot where a message will be triggered every one minute in the channel.

它可以在模拟器中运行,但是不能在网络聊天和MS团队中运行: 通知程序控制器未触发.

Its working in the emulator, however its not working in webchat and MS teams: The notifier controller is not being triggered.

您能帮我吗? 我已将代码上传到GITHUB: https://github.com/nivinsunathree/Botv4.git

Could you please help me on that? I've uploaded the code in GITHUB: https://github.com/nivinsunathree/Botv4.git

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
    await base.OnTurnAsync(turnContext, cancellationToken);


    System.Timers.Timer checkForTime = new System.Timers.Timer(interval60Minutes);
    checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
    checkForTime.Enabled = true;


    // Save any state changes that might have occured during the turn.
    await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
    await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
}

void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
{
    bool timeIsReady = true;
    if (timeIsReady == true)
    {
        var url = "http://localhost:3978/api/notify";

        try
        {
            Process.Start(url);
        }
        catch
        {
            // hack because of this: https://github.com/dotnet/corefx/issues/10361
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                url = url.Replace("&", "^&");
                Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = false });
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                Process.Start("xdg-open", url);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                Process.Start("open", url);
            }
            else
            {
                throw;
            }
        }
    }
}

一分钟后,它会在频道中触发以下消息:

After one minute, it should trigger below message in the channel:

await turnContext.SendActivityAsync(MessageFactory.Text($"Hello!" + Environment.NewLine + $"Trust you are well" + Environment.NewLine + $"Hope that you are having a good day" + Environment.NewLine + $"We are contacting you concerning lea access requests" + Environment.NewLine + $"Could you please review the following tasks if any and add the required information!"), cancellationToken);
//await turnContext.SendActivityAsync(MessageFactory.Text($"Please type ok to continue!"), cancellationToken);
await turnContext.SendActivityAsync(MessageFactory.Text("Could you please click on the below button to continue?"));

var card = new HeroCard
{
    //Text = "Could you please click on the below button to continue?",
    Buttons = new List<CardAction>
        {
            new CardAction(ActionTypes.ImBack, title: "lea access request", value: "lea access request"),
        },
};
var reply = MessageFactory.Attachment(card.ToAttachment());
await turnContext.SendActivityAsync(reply, cancellationToken);

我通过电子邮件提到了此问题,但我将在这里为其他人重新发布:

I mentioned this via email, but I'll repost it here for others:

您的主动消息传递在Emulator中有效,但在Teams中则无效,因为您只在EchoBot.OnConversationUpdateActivityAsync中添加了ConversationReference.

Your proactive messaging works in Emulator but not in Teams because you only add a ConversationReference in EchoBot.OnConversationUpdateActivityAsync.

此处,团队说明仅在安装了漫游器或用户第一次与漫游器对话时才发送ConversationUpdate. 此处是一种测试它的方法.

As explained here, Teams only sends a ConversationUpdate when the bot is installed or a user speaks with the bot the FIRST time EVER. Here is a way you can test it.