-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from mattermost/revert-11
Revert #11
- Loading branch information
Showing
9 changed files
with
405 additions
and
401 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mattermost/mattermost-server/model" | ||
"github.com/mattermost/mattermost-server/plugin" | ||
) | ||
|
||
// RegisterCommand to registers welcomebot commands | ||
func (p *Plugin) RegisterCommand(teamID string) error { | ||
if err := p.API.RegisterCommand(&model.Command{ | ||
TeamId: teamID, | ||
Trigger: welcomeBotUsername, | ||
AutoComplete: true, | ||
AutoCompleteHint: "[team_name]", | ||
AutoCompleteDesc: "Re-runs the welcomebot for the supplied team", | ||
DisplayName: "WelcomeBot Command", | ||
Description: "Re-runs the welcomebot with the supplied team", | ||
}); err != nil { | ||
p.API.LogError( | ||
"failed to register command", | ||
"error", err.Error(), | ||
) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ExecuteCommand executes a command that has been previously registered via the RegisterCommand | ||
// API. | ||
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) { | ||
if !strings.HasPrefix(args.Command, "/"+welcomeBotUsername) { | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: fmt.Sprintf("Unknown command: " + args.Command), | ||
}, nil | ||
} | ||
|
||
team, _ := p.API.GetTeamByName(args.Command) | ||
if team == nil { | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: fmt.Sprintf("Unknown team name: " + args.Command), | ||
}, nil | ||
} | ||
|
||
teamMember, _ := p.API.GetTeamMember(team.Id, args.UserId) | ||
if teamMember == nil { | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: fmt.Sprintf("You do not appear to be part of the team: " + args.Command), | ||
}, nil | ||
} | ||
|
||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: fmt.Sprintf("TODO re-run the WelcomeBot"), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
package main | ||
|
||
const ( | ||
actionTypeAutomatic = "automatic" | ||
actionTypeButton = "button" | ||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/mattermost/mattermost-server/model" | ||
) | ||
|
||
const welcomeBotUsername = "welcomebot" | ||
const actionTypeAutomatic = "automatic" | ||
const actionTypeButton = "button" | ||
|
||
// ConfigMessageAction are actions that can be taken from the welcome message | ||
type ConfigMessageAction struct { | ||
// The action type of button or automatic | ||
|
@@ -62,5 +69,42 @@ func (p *Plugin) OnConfigurationChange() error { | |
|
||
p.welcomeMessages.Store(c.WelcomeMessages) | ||
|
||
if err := p.ensureWelcomeBotUser(); err != nil { | ||
p.API.LogError(err.Error()) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *Plugin) ensureWelcomeBotUser() *model.AppError { | ||
var err *model.AppError | ||
|
||
user, _ := p.API.GetUserByUsername(welcomeBotUsername) | ||
|
||
// Ensure the configured user exists. | ||
if user == nil { | ||
randBytes := make([]byte, 15) | ||
rand.Read(randBytes) | ||
password := base64.StdEncoding.EncodeToString(randBytes) | ||
fmt.Println(password) | ||
|
||
user, err = p.API.CreateUser(&model.User{ | ||
Username: welcomeBotUsername, | ||
Password: password, | ||
Email: fmt.Sprintf("%[email protected]", welcomeBotUsername), | ||
Nickname: "Welcome Bot", | ||
FirstName: "Welcome", | ||
LastName: "Bot", | ||
Position: "Bot", | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
p.welcomeBotUserID = user.Id | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.