Documentation
¶
Overview ¶
The plugin package is used by Mattermost server plugins written in go. It also enables the Mattermost server to manage and interact with the running plugin environment.
Note that this package exports a large number of types prefixed with Z_. These are public only to allow their use with Hashicorp's go-plugin (and net/rpc). Do not use these directly.
Example (HelloWorld) ¶
This example demonstrates a plugin that handles HTTP requests which respond by greeting the world.
package main
import (
"fmt"
"net/http"
"github.com/mattermost/mattermost-server/plugin"
)
type HelloWorldPlugin struct {
plugin.MattermostPlugin
}
func (p *HelloWorldPlugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world!")
}
// This example demonstrates a plugin that handles HTTP requests which respond by greeting the
// world.
func main() {
plugin.ClientMain(&HelloWorldPlugin{})
}
Output:
Example (HelpPlugin) ¶
package main
import (
"strings"
"sync"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
)
// configuration represents the configuration for this plugin as exposed via the Mattermost
// server configuration.
type configuration struct {
TeamName string
ChannelName string
// channelId is resolved when the public configuration fields above change
channelId string
}
type HelpPlugin struct {
plugin.MattermostPlugin
// configurationLock synchronizes access to the configuration.
configurationLock sync.RWMutex
// configuration is the active plugin configuration. Consult getConfiguration and
// setConfiguration for usage.
configuration *configuration
}
// getConfiguration retrieves the active configuration under lock, making it safe to use
// concurrently. The active configuration may change underneath the client of this method, but
// the struct returned by this API call is considered immutable.
func (p *HelpPlugin) getConfiguration() *configuration {
p.configurationLock.RLock()
defer p.configurationLock.RUnlock()
if p.configuration == nil {
return &configuration{}
}
return p.configuration
}
// setConfiguration replaces the active configuration under lock.
//
// Do not call setConfiguration while holding the configurationLock, as sync.Mutex is not
// reentrant.
func (p *HelpPlugin) setConfiguration(configuration *configuration) {
// Replace the active configuration under lock.
p.configurationLock.Lock()
defer p.configurationLock.Unlock()
p.configuration = configuration
}
// OnConfigurationChange updates the active configuration for this plugin under lock.
func (p *HelpPlugin) OnConfigurationChange() error {
var configuration = new(configuration)
// Load the public configuration fields from the Mattermost server configuration.
if err := p.API.LoadPluginConfiguration(configuration); err != nil {
return errors.Wrap(err, "failed to load plugin configuration")
}
team, err := p.API.GetTeamByName(configuration.TeamName)
if err != nil {
return errors.Wrapf(err, "failed to find team %s", configuration.TeamName)
}
channel, err := p.API.GetChannelByName(configuration.ChannelName, team.Id, false)
if err != nil {
return errors.Wrapf(err, "failed to find channel %s", configuration.ChannelName)
}
configuration.channelId = channel.Id
p.setConfiguration(configuration)
return nil
}
func (p *HelpPlugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {
configuration := p.getConfiguration()
// Ignore posts not in the configured channel
if post.ChannelId != configuration.channelId {
return
}
// Ignore posts this plugin made.
if sentByPlugin, _ := post.Props["sent_by_plugin"].(bool); sentByPlugin {
return
}
// Ignore posts without a plea for help.
if !strings.Contains(post.Message, "help") {
return
}
p.API.SendEphemeralPost(post.UserId, &model.Post{
ChannelId: configuration.channelId,
Message: "You asked for help? Checkout https://about.mattermost.com/help/",
Props: map[string]interface{}{
"sent_by_plugin": true,
},
})
}
func main() {
plugin.ClientMain(&HelpPlugin{})
}
Output:
Index ¶
- Constants
- func ClientMain(pluginImplementation interface{})
- func IsValidId(id string) bool
- type API
- type Context
- type Environment
- func (env *Environment) Activate(id string) (manifest *model.Manifest, activated bool, reterr error)
- func (env *Environment) Active() []*model.BundleInfo
- func (env *Environment) Available() ([]*model.BundleInfo, error)
- func (env *Environment) Deactivate(id string) bool
- func (env *Environment) HooksForPlugin(id string) (Hooks, error)
- func (env *Environment) IsActive(id string) bool
- func (env *Environment) RunMultiPluginHook(hookRunnerFunc func(hooks Hooks) bool, hookId int)
- func (env *Environment) Shutdown()
- func (env *Environment) Statuses() (model.PluginStatuses, error)
- type ErrorString
- type Hooks
- type MattermostPlugin
- type Z_AddChannelMemberArgs
- type Z_AddChannelMemberReturns
- type Z_AddReactionArgs
- type Z_AddReactionReturns
- type Z_ChannelHasBeenCreatedArgs
- type Z_ChannelHasBeenCreatedReturns
- type Z_CopyFileInfosArgs
- type Z_CopyFileInfosReturns
- type Z_CreateBotArgs
- type Z_CreateBotReturns
- type Z_CreateChannelArgs
- type Z_CreateChannelReturns
- type Z_CreatePostArgs
- type Z_CreatePostReturns
- type Z_CreateTeamArgs
- type Z_CreateTeamMemberArgs
- type Z_CreateTeamMemberReturns
- type Z_CreateTeamMembersArgs
- type Z_CreateTeamMembersReturns
- type Z_CreateTeamReturns
- type Z_CreateUserArgs
- type Z_CreateUserReturns
- type Z_DeleteChannelArgs
- type Z_DeleteChannelMemberArgs
- type Z_DeleteChannelMemberReturns
- type Z_DeleteChannelReturns
- type Z_DeleteEphemeralPostArgs
- type Z_DeleteEphemeralPostReturns
- type Z_DeletePostArgs
- type Z_DeletePostReturns
- type Z_DeleteTeamArgs
- type Z_DeleteTeamMemberArgs
- type Z_DeleteTeamMemberReturns
- type Z_DeleteTeamReturns
- type Z_DeleteUserArgs
- type Z_DeleteUserReturns
- type Z_DisablePluginArgs
- type Z_DisablePluginReturns
- type Z_EnablePluginArgs
- type Z_EnablePluginReturns
- type Z_ExecuteCommandArgs
- type Z_ExecuteCommandReturns
- type Z_FileWillBeUploadedArgs
- type Z_FileWillBeUploadedReturns
- type Z_GetBotArgs
- type Z_GetBotReturns
- type Z_GetBotsArgs
- type Z_GetBotsReturns
- type Z_GetBundlePathArgs
- type Z_GetBundlePathReturns
- type Z_GetChannelArgs
- type Z_GetChannelByNameArgs
- type Z_GetChannelByNameForTeamNameArgs
- type Z_GetChannelByNameForTeamNameReturns
- type Z_GetChannelByNameReturns
- type Z_GetChannelMemberArgs
- type Z_GetChannelMemberReturns
- type Z_GetChannelMembersArgs
- type Z_GetChannelMembersByIdsArgs
- type Z_GetChannelMembersByIdsReturns
- type Z_GetChannelMembersForUserArgs
- type Z_GetChannelMembersForUserReturns
- type Z_GetChannelMembersReturns
- type Z_GetChannelReturns
- type Z_GetChannelStatsArgs
- type Z_GetChannelStatsReturns
- type Z_GetChannelsForTeamForUserArgs
- type Z_GetChannelsForTeamForUserReturns
- type Z_GetConfigArgs
- type Z_GetConfigReturns
- type Z_GetDiagnosticIdArgs
- type Z_GetDiagnosticIdReturns
- type Z_GetDirectChannelArgs
- type Z_GetDirectChannelReturns
- type Z_GetEmojiArgs
- type Z_GetEmojiByNameArgs
- type Z_GetEmojiByNameReturns
- type Z_GetEmojiImageArgs
- type Z_GetEmojiImageReturns
- type Z_GetEmojiListArgs
- type Z_GetEmojiListReturns
- type Z_GetEmojiReturns
- type Z_GetFileArgs
- type Z_GetFileInfoArgs
- type Z_GetFileInfoReturns
- type Z_GetFileLinkArgs
- type Z_GetFileLinkReturns
- type Z_GetFileReturns
- type Z_GetGroupChannelArgs
- type Z_GetGroupChannelReturns
- type Z_GetLDAPUserAttributesArgs
- type Z_GetLDAPUserAttributesReturns
- type Z_GetLicenseArgs
- type Z_GetLicenseReturns