0
votes

I'm working on a bot for Microsoft Teams. I am using the custom bot feature. I got the bot working as a sideloaded package, but due to the constraints of my network, I need to keep the bot internal and use the custom bot feature. I am currently testing it by using ngrok to tunnel to my localhost.

I am now running into an issue when I try to create my reply. Whenever I call this:

var reply = activity.CreateReply(message.ReadToEnd());

I get a NullReferenceException saying that the "Object reference not set to an instance of an object". message is an open .txt file. I get this error every time I call activity.CreateReply(). The part that I don't understand is that everything works as intended in the Bot Framework Emulator and when the bot is a sideloaded package, but not when the bot is a custom bot.

Here's my full Post method:

    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

        if (activity.Type == ActivityTypes.Message)
        {
            // Commands:
            // Retrieve TFS Work Item(s)
            if (new Regex(@"\but\s?\d{5}\b").IsMatch(activity.Text.ToLower()))
            {
                var reply = new RetrieveWorkItem();
                await connector.Conversations.ReplyToActivityAsync(reply.Response(activity));
            }
            // Help
            else if (activity.Text.ToLower().Contains("help"))
            {
                var message = File.OpenText($"{System.AppDomain.CurrentDomain.BaseDirectory}/Messages/HelpMessage.txt");
                var reply = activity.CreateReply(message.ReadToEnd());
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
            // Not Recognized
            else
            {
                var reply = activity.CreateReply("Command not recognized. Type \"@Keller Bot Help\" for a list of commands.");
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
        }
        else
        {
            HandleSystemMessage(activity, connector);
        }

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

Here's the full error and StackTrace sent by the bot: https://pastebin.com/ZSrjrA9z

1
are you sure that the file is being opened ok?Ezequiel Jadib
Yes, the issue is not with the file because it opens just fine when the bot is sideloaded or being ran from the emulator using the exact same callback URL. I didn't mean to call much attention attention to message, as all of my activity.CreateReply() calls throw the same exception, including the one under // Not Recognized that takes in a simple string.Evan Ronnei
is anyone able to reproduce this? I tried and I cannotD4RKCIDE
What do you have into your reply object ?Bob Swager
It is either a message containing only a string, or a message with some thumbnail cards attached to it. Neither work.Evan Ronnei

1 Answers

0
votes

You say you're implementing this as a custom bot, per the instructions here. The issue is that it appears as if you're using the Bot Framework messaging calls (e.g. CreateReply()), which won't work since you're not dealing with a registered BF bot when you go through the custom bot process.

Instead, you can just create a new Activity() and return that in response to the HttpPost request.

We do have a sample you can check out, in case that helps.