Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MM-382] Add feature to use struct fields in the channel welcome message #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[MM-382] Review fixes: Use p.client and update readme
ayusht2810 committed Jul 11, 2024
commit 3cc7637ef88ce09ba5275e2d125b9c0b51ab446e
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -83,12 +83,13 @@ The preview of the configured messages, as well as the creation of a channel wel
* `/welcomebot help` - Displays usage information.
* `/welcomebot list` - Lists the teams for which greetings were defined.
* `/welcomebot preview [team-name]` - Sends ephemeral messages to the user calling the command, with the preview of the welcome message[s] for the given team name and the user that requested the preview.
* `/welcomebot set_channel_welcome [welcome-message]` - Sets the given text as current's channel welcome message. You can also include `{{.UserDisplayName}}` and `{{.Channel.DisplayName}}` in the command to print the user joined and the channel display name in the message, respectively. You can insert any variable from the `MessageTemplate` struct, which has the following fields:
* `/welcomebot set_channel_welcome [welcome-message]` - Sets the given text as current's channel welcome message. You can also include `{{.UserDisplayName}}` and `{{.Channel.DisplayName}}` in the `welcome-message` to print the user joined and the channel display name in the message, respectively. You can insert any variable from the `MessageTemplate` struct, which has the following fields:
```go
type MessageTemplate struct {
WelcomeBot *model.User
User *model.User
DirectMessage *model.Channel
Channel *model.Channel
UserDisplayName string
}
```
18 changes: 9 additions & 9 deletions server/welcomebot.go
Original file line number Diff line number Diff line change
@@ -47,26 +47,26 @@ func (p *Plugin) constructMessageTemplate(userID, teamID string) *MessageTemplat

func (p *Plugin) constructChannelMessageTemplate(userID, channelID string) *ChannelMessageTemplate {
data := &ChannelMessageTemplate{}
var err *model.AppError
var err error

if len(userID) > 0 {
if data.User, err = p.API.GetUser(userID); err != nil {
p.API.LogError("failed to query user", "UserID", userID)
if data.User, err = p.client.User.Get(userID); err != nil {
p.client.Log.Error("failed to query user", "UserID", userID)
return nil
}
}

if data.User != nil {
if data.DirectMessage, err = p.API.GetDirectChannel(userID, p.botUserID); err != nil {
p.API.LogError("failed to query direct message channel", "UserID", userID)
if data.DirectMessage, err = p.client.Channel.GetDirect(userID, p.botUserID); err != nil {
p.client.Log.Error("failed to query direct message channel", "UserID", userID)
return nil
}
}

if len(channelID) > 0 {
data.Channel, err = p.API.GetChannel(channelID)
data.Channel, err = p.client.Channel.Get(channelID)
if err != nil {
p.API.LogError("failed to query channel", "ChannelID", channelID)
p.client.Log.Error("failed to query channel", "ChannelID", channelID)
return nil
}
}
@@ -203,8 +203,8 @@ func (p *Plugin) renderWelcomeMessage(messageTemplate MessageTemplate, configMes
func (p *Plugin) getTemplateMessage(data []string, messageTemplate interface{}) string {
tmpMsg, _ := template.New("Response").Parse(strings.Join(data, "\n"))
var message bytes.Buffer
if appErr := tmpMsg.Execute(&message, messageTemplate); appErr != nil {
p.API.LogError("Failed to execute channel message template", "Error", appErr.Error())
if err := tmpMsg.Execute(&message, messageTemplate); err != nil {
p.client.Log.Error("Failed to execute channel message template", "Error", err.Error())
return ""
}