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

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!]