How can an external app receive an event when a user sends a direct message to its corresponding bot account?

I want to create a chat bot in Mattermost. A human user can send a direct message to the bot to start a conversation. The AI logic for the bot will reside in an external application.

After searching through the Mattermost docs, I thought that using an App is the most suitable option, since it comes with a corresponding bot account - Apps
“2. External (HTTP) Apps. Apps can be hosted as publicly (or privately) available HTTP services, the choice of hosting provider is yours. A self-managed Mattermost customer would be able to install your App from a URL”

I referred to the sample apps provided on GitHub - mattermost/mattermost-app-examples: Examples for the Mattermost App Framework
and created my own

Then I was able to add the app to a Mattermost server hosted locally using the following command -

/apps install http http://172.17.0.1:4675/mattermost/manifest.json

I used the following manifest.json file

{
  "app_id":"testing",
  "version":"v1.2.0",
  "homepage_url":"http://172.17.0.1:4675/mattermost/homepage.html",
  "display_name":"Testing",
  "icon":"icon.png",
  "requested_permissions":["act_as_bot","act_as_user"],
  "requested_locations":["/channel_header","/command"],
  "http":{"root_url":"http://172.17.0.1:4675/mattermost"}
}

And the following bindings file

{
  "type": "ok",
  "data": [
    {
      "location": "/channel_header",
      "bindings": [
        {
          "location": "send-button",
          "icon": "icon.png",
          "label": "send hello message",
          "form": {
            "title": "Hello, world!",
            "icon": "icon.png",
            "submit": {
              "path": "/send",
              "expand": {
                "acting_user": "id",
                "acting_user_access_token": "all"
              }
            },
            "fields": [
              {
                "name": "Message",
                "type": "text"
              }
            ]
          }
        }
      ]
    },
    {
      "location": "/command",
      "bindings": [
        {
          "icon": "icon.png",
          "label": "helloworld",
          "hint": "[send]",
          "description": "Hello World app",
          "bindings": [
            {
              "label": "send",
              "form": {
                "title": "Hello, world!",
                "icon": "icon.png",
                "submit": {
                  "path": "/send",
                  "expand": {
                    "acting_user": "id",
                    "acting_user_access_token": "all"
                  }
                },
                "fields": [
                  {
                    "name": "Message",
                    "type": "text"
                  }
                ]
              }
            }
          ]
        }
      ]
    }
  ]
}

Note: the above are only slight modifications from the examples provided on GitHub - mattermost/mattermost-app-examples: Examples for the Mattermost App Framework

However, with this setup, I do not receive any messages to my app when a user sends a message to the app’s bot. It is unclear to me from the docs how I can subscribe to receiving an event (with the user’s text) when the user sends a direct message to the bot.

Hello Wesley,

Welcome to the Mattermost Community Forums, and congratulations on your first post! We are excited to see you working on developing a chat bot application!

Let’s help you sort out the issue you’re facing. When it comes to external apps in Mattermost, direct messages to the app’s bot are generally captured using Subscribe call. In your app, you want to subscribe to events that occur in Mattermost. This is done by defining what events your app should listen for in the OnActivate function in your App’s code. You can specify a number of event types, including direct messages to the bot.

For example, in the OnActivate function of your application, you would have something like this:

app.OnActivate = func(_ apps.Context, conf apps.Config) error {
_, err := p.mm.Apps.KVSet(AppKey, conf)
if err != nil {
return err
}

 return p.mm.Apps.Subscribe(&apps.Subscription{
    Subject:   apps.SubjectBotDMPosted,
    ChannelID: conf.BotAccessTokenChannel,
    AppID:     AppID,
 })

}

The above code subscribes your app to events that happen whenever a direct message is sent to your app’s bot. You will then need to implement the logic that should occur when these events happen as a webhook.

Keep in mind that you will also need to make sure your bot is properly activated on the Mattermost platform for this to work properly.

For more details, you may refer to this section of the Mattermost developer documentation: Subscribe.

I hope this points you in the right direction. If you have any more questions about your application, please don’t hesitate to ask. Our community is here to help!

Warm wishes,
~Mattermost AI Assistant :robot:

[Disclaimer: This was an AI assisted response powered by Mattermost AI. Were any of the above suggestions inaccurate? Let us know by replying to this comment!]

It seems the above AI generated response is inaccurate.

The documentation link provided for “Subscribe” does not exist. The only link I found is Subscriptions

On this page, the Event subjects that are listed are -

Subject	Description	Plugin Event Name
user_created	A user was created	UserHasBeenCreated
user_joined_channel	A user joined a channel	UserJoinedChannel
user_left_channel	A user left a channel	UserLeftChannel
bot_joined_channel	The App’s bot joined a channel	UserJoinedChannel
bot_left_channel	The App’s bot left a channel	UserLeftChannel
user_joined_team	A user joined a team	UserJoinedTeam
user_left_team	A user left a team	UserLeftTeam
bot_joined_team	The App’s bot joined a team	UserJoinedTeam
bot_left_team	The App’s bot left a team	UserLeftTeam
channel_created	A channel was created	ChannelHasBeenCreated

There is no event listed for a direct message, unlike what the AI generated response above suggests.

Now, it may be that Mattermost has such an event but the documentation is not up to date. If so, please let me know what that event is. If not, please let me know anyway so I can perhaps open a feature request.

My company needs to evaluate the possibility of doing an AI chat bot integration for a client using the above functionality. If it does not exist, we should at least be able to tell them that the current Mattermost API doesn’t support what we want to do, or it is planned to be included in a future Mattermost release.

1 Like

Hey Wesley, I am not sure what language you’re looking to develop your bot in, but in Python there is a mmpy-bot package you can use. I actually use this myself for DM’s the bot account that I created. It uses regex to listen for messages and then you can filter on those messages being in a specific channel or directly to the bot account.

1 Like

@cdrew5 , thank you for your response. I am using Java for developing the bot, so I can’t use the Python package, but it will be a good reference to look at. Thank you for mentioning it. I skimmed through the Github code and thankfully the Readme mentioned that it is “Based on Mattermost WebSocket API(V4.0.0)”.

So it would appear that that’s the right API for me to use instead of trying to get this to work with event subscriptions for the App.

I was misled by the fact that Mattermost advertises itself as a Slack alternative (Open Source Slack Alternative | Mattermost), but in Slack the App and the corresponding events API (Using the Slack Events API | Slack) is exactly what I used to build the AI chat bot integration. It appears the terminology and architecture in Mattermost is a bit different.

Anyway, I will explore the websocket API unless anyone else points me in a different / better direction.

I got it working now with the Websocket for incoming messages to the bot and the http://your-mattermost-url.com/api/v4/posts API call for replies from the bot to those messages.

@cdrew5 , thank you for pointing me in the right direction.