github.com/turgay/mattermost-server@v5.3.2-0.20181002173352-2945e8a2b0ce+incompatible/plugin/example_help_test.go (about) 1 package plugin_test 2 3 import ( 4 "strings" 5 6 "github.com/mattermost/mattermost-server/model" 7 "github.com/mattermost/mattermost-server/plugin" 8 ) 9 10 type HelpPlugin struct { 11 plugin.MattermostPlugin 12 13 TeamName string 14 ChannelName string 15 16 channelId string 17 } 18 19 func (p *HelpPlugin) OnConfigurationChange() error { 20 // Reuse the default implementation of OnConfigurationChange to automatically load the 21 // required TeamName and ChannelName. 22 if err := p.MattermostPlugin.OnConfigurationChange(); err != nil { 23 p.API.LogError(err.Error()) 24 return nil 25 } 26 27 team, err := p.API.GetTeamByName(p.TeamName) 28 if err != nil { 29 p.API.LogError("failed to find team", "team_name", p.TeamName) 30 return nil 31 } 32 33 channel, err := p.API.GetChannelByName(p.ChannelName, team.Id, false) 34 if err != nil { 35 p.API.LogError("failed to find channel", "channel_name", p.ChannelName) 36 return nil 37 } 38 39 p.channelId = channel.Id 40 41 return nil 42 } 43 44 func (p *HelpPlugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) { 45 // Ignore posts not in the configured channel 46 if post.ChannelId != p.channelId { 47 return 48 } 49 50 // Ignore posts this plugin made. 51 if sentByPlugin, _ := post.Props["sent_by_plugin"].(bool); sentByPlugin { 52 return 53 } 54 55 // Ignore posts without a plea for help. 56 if !strings.Contains(post.Message, "help") { 57 return 58 } 59 60 p.API.SendEphemeralPost(post.UserId, &model.Post{ 61 ChannelId: p.channelId, 62 Message: "You asked for help? Checkout https://about.mattermost.com/help/", 63 Props: map[string]interface{}{ 64 "sent_by_plugin": true, 65 }, 66 }) 67 } 68 69 func Example_helpPlugin() { 70 plugin.ClientMain(&HelpPlugin{}) 71 }