Search code examples
c#.netvisual-studioskypeskypedeveloper

c# - Cannot get Skype Bot to respond to the proper event


I am using Visual Studio to create a Skype bot based on their Bot Builder SDK and using the Skype emulator. (I am using this page) I successfully got the bot to connect and receive regular text messages, and it responds properly with:

You sent [message] which was [length] characters

However, I tried adding an event to fire when a user is added, and it should just send "Welcome".

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{

        if (activity.Type == ActivityTypes.Message)
        {

            ConnectorClient connector = new ConnectorClient(new System.Uri(activity.ServiceUrl));

            int length = (activity.Text ?? string.Empty).Length;


            Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");

            await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
        }

        else
        {

            HandleSystemMessage(activity);

        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
}

private Activity HandleSystemMessage(Activity message)
{
    //The other if statements are the rest of the activity types
    if (message.Type == ActivityTypes.ConversationUpdate)
    {
        // Handle conversation state changes, like members being added and removed
        // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
        // Not available in all channels

        Activity reply = message.CreateReply("Welcome");

    }

This is the code I have currently. I tried adding the same Connector and await lines to the else statement on line 17, but that just makes the bot respond with

You sent which was 0 characters

If any other info is needed to fix this I'll be happy to provide it.

Any help is appreciated!

EDIT: The code I currently have does not respond with anything. It sees the ConversationUpdate event and does nothing with it


Solution

  • I've never used the Skype SDK but I imagine it would look something like this if you did it properly:

    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
        Activity reply = null;
    
        if (activity.Type == ActivityTypes.Message)
        {
            int length = (activity.Text ?? string.Empty).Length;
            reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
        }
        else if (activity.Type == ActivityTypes.ConversationUpdate)
        {
            reply = message.CreateReply("Welcome");
        }
    
        if (reply != null) 
        {
            await connector.Conversations.SendToConversationAsync((Activity)reply);
        }
    }
    

    I'd suggest you take a tutorial in C# and maybe read the skype documentation? https://learn.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-connector


    The above still may not work, if you take a look at the docs link I posted you may need to start a conversation if one does not already exist using the CreateDirectConversationAsync method.