Initiate a message from bot to user on BotFramework

后端 未结 2 1910
谎友^
谎友^ 2020-12-06 01:29

I have a bot built on BotFramework 3.5 and hosted on Azure as a WebApp. I didn\'t face any problems with implementation of scenarios where the bot needs to respond to user\'

相关标签:
2条回答
  • 2020-12-06 02:08

    Try creating a new conversation like this:

    var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);
    
    0 讨论(0)
  • 2020-12-06 02:12

    According to your description, I followed this tutorial for getting started with the Connector and followed this tutorial for sending and Receiving Activities.

    Based on your code, I created my console application and I could reproduce the same issue, then I found a git issue about the similar issue. After some trials, I could make it work as expected on my side, you could refer to it:

    MicrosoftAppCredentials.TrustServiceUrl("{ServiceUrl}", DateTime.Now.AddDays(7));
    var account=new MicrosoftAppCredentials("MicrosoftAppIdKey", "MicrosoftAppPasswordKey");
    var connector = new ConnectorClient(new Uri("{ServiceUrl}"),account);
    

    OR

    Implement your DelegatingHandler

    public class MyDelegatingHandler : DelegatingHandler
    {
        private string _token;
        public MyDelegatingHandler(string token)
        {
            _token = token;
        }
    
        protected override Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
        {
            request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _token);
            return base.SendAsync(request, cancellationToken);
        }
    }
    

    Then, you need to build your ConnectorClient as follows:

    var account=new MicrosoftAppCredentials("{MicrosoftAppIdKey}", "{MicrosoftAppPasswordKey}");
    var jwtToken=await account.GetTokenAsync();
    var connector = new ConnectorClient(new Uri("{serviceUrl}"),handlers:new MyDelegatingHandler(jwtToken));
    

    Here is my console application code snippet, you could refer to it:

    try
    {
        var userAccount = new ChannelAccount() { Id = "default-user", Name = "user" };
        var botAccount = new ChannelAccount() { Id = "934493jn5f6f348f", Name = "console-Bot" };
        string url = "{serviceUrl}";
    
        MicrosoftAppCredentials.TrustServiceUrl(url, DateTime.Now.AddDays(7));
        var account = new MicrosoftAppCredentials("{MicrosoftAppIdKey}", "{MicrosoftAppPasswordKey}");
        var connector = new ConnectorClient(new Uri(url), account);
    
        IMessageActivity message = Activity.CreateMessageActivity();
        message.From = botAccount;
        message.Recipient = userAccount;
        message.Conversation = new ConversationAccount() { Id = "{conversationId}" };
        message.Text = "Message sent from console application!!!";
        message.Locale = "en-us";
        var response = await connector.Conversations.SendToConversationAsync((Activity)message);
        Console.WriteLine($"response:{response.Id}");
    }
    catch (Exception e)
    {
        Console.WriteLine($"exception:{e.Message}\r\n{e.StackTrace}");
    }
    

    Result

    0 讨论(0)
提交回复
热议问题