Sending proactive messages to ms teams from azure deployed bot

与世无争的帅哥 提交于 2020-06-16 12:48:49

问题


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.

Connecting to MS Teams

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

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

回答1:


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

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

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.



来源:https://stackoverflow.com/questions/57973996/sending-proactive-messages-to-ms-teams-from-azure-deployed-bot

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