Proactively sending message through bot using a web app

徘徊边缘 提交于 2019-12-06 11:50:16

It seems you are struggling with the same issue I had last week. it seems that the CreateDirectConversationAsync does not work in MS Teams as Teams also needs a tennantId. I found a statement about this here: https://github.com/Microsoft/BotBuilder/issues/2944

the answer mentions a nuget package (Microsoft.Bot.Connector.Teams) that is no longer available in V4 of the SDK. but as I see that you already got a conversationId from your JSON input, this should not be a problem. just use the conversationId you passed in the JSON. if you would do this, your code could look something like:

private static async Task SendProActiveMessgae()private async Task ForwardMessage(JToken forwardContext, string messageToSend)
{
    // Collect data from JSON input
    var restCmd = forwardContext;
    var toId = (string) restCmd["toId"];
    var toName = (string) restCmd["toName"];
    var fromId = (string) restCmd["fromId"];
    var fromName = (string) restCmd["fromName"];
    var serviceURL = (string) restCmd["serviceURL"]
    var conversationId = (string) restCmd["conversation"];

    var uri = new Uri(serviceURL);
    var appId = "APP ID";
    var appSecret = "APP PASSWORD";
    ConnectorClient connector = new ConnectorClient(uri, appId, appSecret);

    var activity = new Activity()
    {
        Type = ActivityTypes.Message,
        From = new ChannelAccount(fromId, fromName),
        Recipient = new ChannelAccount(toId, toName),
        Conversation = new ConversationAccount(false, "personal", conversationId),
        Text = messageToSend
    };
    try
    {
        MicrosoftAppCredentials.TrustServiceUrl(serviceURL);
        await connector.Conversations.SendToConversationAsync(conversationId, activity);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!