Implement the message placeholder replacer

This commit is contained in:
Alphyron 2020-06-27 18:38:59 +02:00
parent f2d964a2f3
commit 1a8a0a2db8
9 changed files with 32 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package channels
import ( import (
"git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/logic"
"git.dragon-labs.de/alphyron/group_helper/message"
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"log" "log"
@ -87,7 +88,7 @@ func checkGroupUser(group *models.Group, groupHelperService logic.GroupHelperSer
} }
leaveMessage := group.UserLeaveMessage leaveMessage := group.UserLeaveMessage
//TODO Replace placeholder leaveMessage = message.ReplaceMessage(leaveMessage, member.User)
msg := tgbotapi.NewMessage(group.GroupID, leaveMessage) msg := tgbotapi.NewMessage(group.GroupID, leaveMessage)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"

View File

@ -2,6 +2,7 @@ package channels
import ( import (
"git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/logic"
"git.dragon-labs.de/alphyron/group_helper/message"
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/obj" "git.dragon-labs.de/alphyron/group_helper/obj"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
@ -73,7 +74,7 @@ func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, group
} }
kickMessage := groups[user.GroupID].UserKickMessage kickMessage := groups[user.GroupID].UserKickMessage
//TODO Replace placeholder kickMessage = message.ReplaceMessage(kickMessage, member.User)
msg := tgbotapi.NewMessage(user.GroupID, kickMessage) msg := tgbotapi.NewMessage(user.GroupID, kickMessage)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/logic"
message2 "git.dragon-labs.de/alphyron/group_helper/message"
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings" "strings"
@ -61,8 +62,7 @@ func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat
return false, err return false, err
} }
//TODO Create a replace for placeholder msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserJoinMessage, update.Message.From))
msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example JoinMessage")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example JoinMessage"))
_, err = api.Send(msg) _, err = api.Send(msg)

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/logic"
message2 "git.dragon-labs.de/alphyron/group_helper/message"
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings" "strings"
@ -61,8 +62,7 @@ func (j kickMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat
return false, err return false, err
} }
//TODO Create a replace for placeholder msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserKickMessage, update.Message.From))
msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserKickMessage)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example KickMessage")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example KickMessage"))
_, err = api.Send(msg) _, err = api.Send(msg)

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/logic"
message2 "git.dragon-labs.de/alphyron/group_helper/message"
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings" "strings"
@ -61,8 +62,7 @@ func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Upda
return false, err return false, err
} }
//TODO Create a replace for placeholder msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserLeaveMessage, update.Message.From))
msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserLeaveMessage)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example LeaveMessage")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example LeaveMessage"))
_, err = api.Send(msg) _, err = api.Send(msg)

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/logic"
message2 "git.dragon-labs.de/alphyron/group_helper/message"
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings" "strings"
@ -61,8 +62,7 @@ func (j verifiedMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.U
return false, err return false, err
} }
//TODO Create a replace for placeholder msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserVerifiedMessage, update.Message.From))
msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserVerifiedMessage)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example VerifiedMessage")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example VerifiedMessage"))
_, err = api.Send(msg) _, err = api.Send(msg)

16
message/replace.go Normal file
View File

@ -0,0 +1,16 @@
package message
import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strconv"
"strings"
)
func ReplaceMessage(message string, user *tgbotapi.User) string {
message = strings.Replace(message, "{{user.firstname}}", user.FirstName, -1)
message = strings.Replace(message, "{{user.lastname}}", user.LastName, -1)
message = strings.Replace(message, "{{user.username}}", user.UserName, -1)
message = strings.Replace(message, "{{user.name}}", user.UserName, -1)
message = strings.Replace(message, "{{user.id}}", strconv.Itoa(user.ID), -1)
return message
}

View File

@ -1,6 +1,7 @@
package routines package routines
import ( import (
message2 "git.dragon-labs.de/alphyron/group_helper/message"
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/obj" "git.dragon-labs.de/alphyron/group_helper/obj"
"git.dragon-labs.de/alphyron/group_helper/telegram" "git.dragon-labs.de/alphyron/group_helper/telegram"
@ -47,8 +48,7 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd
markup := tgbotapi.NewInlineKeyboardMarkup(tasks...) markup := tgbotapi.NewInlineKeyboardMarkup(tasks...)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage) msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserJoinMessage, update.Message.From))
//TODO Replace placeholder
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
msg.ReplyMarkup = markup msg.ReplyMarkup = markup

View File

@ -2,6 +2,7 @@ package routines
import ( import (
"git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/logic"
message2 "git.dragon-labs.de/alphyron/group_helper/message"
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/obj" "git.dragon-labs.de/alphyron/group_helper/obj"
"git.dragon-labs.de/alphyron/group_helper/telegram" "git.dragon-labs.de/alphyron/group_helper/telegram"
@ -52,9 +53,7 @@ func (v verifierRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update
botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are now verified. Welcome to the chat!")) botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are now verified. Welcome to the chat!"))
verifyMessage := group.UserVerifiedMessage verifyMessage := group.UserVerifiedMessage
//TODO Replace placeholder msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, message2.ReplaceMessage(verifyMessage, update.Message.From))
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, verifyMessage)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
_, err := botAPI.Send(msg) _, err := botAPI.Send(msg)