简体   繁体   中英

Sending message from bot to a Skype User using Botframework Version 3

Updated

I am developing a Skype bot with 1:1 conversation with Bot Framework. In that I have a WebHook method which will call from an external service and sends message to my bot, then my bot will send that message to a skype user.

The following code is for v1 in message controller along with api/messages post method

public async Task<Message> Post([FromBody]Message message){}

[Route("~/api/messages/hook")]
    [HttpPost]
    public async Task<IHttpActionResult> WebHook([FromBody]WebHookMessage message)
    {
        if (message.Type == "EmotionUpdate")
        {
            const string fromBotAddress = "<Skype Bot ID here>";
            const string toBotAddress = "<Destination Skype name here>";
            var text = resolveEmoji(message.Data);

            using (var client = new ConnectorClient())
            {
                var outMessage = new Message
                {
                    To = new ChannelAccount("skype", address: toBotAddress , isBot: false),
                    From = new ChannelAccount("skype", address: $"8:{fromBotAddress}", isBot: true),
                    Text = text,
                    Language = "en",

                };

                await client.Messages.SendMessageAsync(outMessage);
            }
        }
        return Ok();
    }

I will call above WebHook from another service, so that my bot will send messages to the respective skype user.

Can anyone please help me how can I achieve the same in V3 bot framework? I tried the following but not working

const string fromBotAddress = "Microsoft App ID of my bot";
        const string toBotAddress = "skype username";
        WebHookMessage processedData = JsonConvert.DeserializeObject<WebHookMessage>(message);
        var text = resolveEmoji(processedData.Data);

        using (var client = new ConnectorClient(new Uri("https://botname.azurewebsites.net/")
            , "Bot Microsoft App Id", "Bot Microsoft App secret",null))
        {
            var outMessage = new Activity
            {
                ReplyToId = toBotAddress,
                From = new ChannelAccount("skype", $"8:{fromBotAddress}"),
                Text = text

            };

            await client.Conversations.SendToConversationAsync(outMessage);
        }

But it is not working, finally what I want to achieve is I want my bot send a message to a user any time how we will send message to a person in skype.

The following code works, but there are some things that are not that obvious that I figured out (tested on Skype channel)

When a user interacts with the bot the user is allocated an id that can only be used from a specific bot..for example: I have multiple bots each using a skype channel. When I send a message from my skype user to bot A the id is different than for bot B. In the previous version of the bot framework I could just send a message to my real skype user id, but not anymore. In a way it simplifies the whole process because you only need the recipient's id and the framework takes care of the rest, so you don't have to specify a sender or bot Id (I guessed all that is linked behind the scenes)

    [Route("OutboundMessages/Skype")]
    [HttpPost]
    public async Task<HttpResponseMessage> SendSkypeMessage(SkypePayload payload)
    {                   

        using (var client = new ConnectorClient(new Uri("https://skype.botframework.com")))
        {
            var conversation = await client.Conversations.CreateDirectConversationAsync(new ChannelAccount(), new ChannelAccount(payload.ToSkypeId));
            IMessageActivity message = Activity.CreateMessageActivity();
            message.From = new ChannelAccount();
            message.Recipient = new ChannelAccount(payload.ToSkypeId);
            message.Conversation = new ConversationAccount { Id= conversation.Id };
            message.Text = payload.MessageBody;             
            await client.Conversations.SendToConversationAsync((Activity)message);
        }

        return Request.CreateResponse(HttpStatusCode.OK);
    }

I'm not sure I understand what you're trying to do. If you'd like to answer a message ( activity ), try something like this:

ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
var reply = activity.createReply(text, "en");
await connector.Conversations.ReplyToActivityAsync(reply);

Activity.createReply switches the From and Recipient fields from the incoming activity. You can also try setting these field manually.

UPDATE

You need to create a ConnectorClient to the Skype Connector Service, not to your bot! So try with the Uri http://skype.botframework.com it might work.

However, I don't think you can message a user on Skype without receiving a message from it in the first place (ie your bot needs to be added to the user's contacts). Once you have an incoming message from the user, you can use it the create replies, just as described above.

WebHookMessage processedData = JsonConvert.DeserializeObject<WebHookMessage>(message);
var text = resolveEmoji(processedData.Data);

var client = new ConnectorClient(new Uri(activity.serviceUrl));
var outMessage = activity.createReply(text);
await client.Conversations.SendToConversationAsync(outMessage);

activity is a message received from the given user earlier. In this case, activity.serviceUrl should be http://skype.botframework.com , but generally you should not rely on this.

You can try to create the activity ( outMessage ) manually; for that, I'd recommend inspecting the From and Recipient fields of a message coming from a Skype user and setting these fields accordingly. However, as mentioned before, your bot needs to be added to the user's contacts, so at this point it will have received a message from the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM