Skip to content

Commit

Permalink
Changed company message format (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Gridnev <[email protected]>
  • Loading branch information
gibiw and Dmitry Gridnev authored Oct 3, 2023
1 parent 7d7f83e commit b1a08f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
28 changes: 10 additions & 18 deletions internal/transport/telegram/commands/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"strings"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/testit-tms/webhook-bot/internal/entities"
Expand All @@ -30,14 +29,14 @@ func (c *CompanyCommands) GetMyCompanies(m *tgbotapi.Message) (tgbotapi.MessageC
const op = "CompanyCommands.GetMyCompanies"

msg := tgbotapi.NewMessage(m.Chat.ID, "")
msg.ParseMode = tgbotapi.ModeMarkdownV2
msg.ParseMode = tgbotapi.ModeHTML
company, err := c.cu.GetCompanyByOwnerTelegramId(context.Background(), m.From.ID)
if err != nil {
if errors.Is(err, usecases.ErrCompanyNotFound) {
msg.Text = `
*You have no companies*
<b>You have no companies</b>
You can register new company with */register* command
You can register new company with <b>/register</b> command
`
return msg, nil
}
Expand All @@ -47,25 +46,18 @@ func (c *CompanyCommands) GetMyCompanies(m *tgbotapi.Message) (tgbotapi.MessageC

// TODO: add more better formatting and add Chats
msg.Text = fmt.Sprintf(`
*Your company:*
*Name:* _%s_
*Email:* _%s_
*Token:* _%s_
`, replaceSpecialCharacters(company.Name), strings.Replace(company.Email, ".", "\\.", -1), company.Token)
<b>Your company:</b>
<b>Name:</b> <i>%s</i>
<b>Email:</b> <i>%s</i>
<b>Token:</b> <i>%s</i>
`, company.Name, company.Email, company.Token)

if len(company.ChatIds) > 0 {
msg.Text += "\n*Chats:*"
msg.Text += "\n<b>Chats:</b>"
for _, chatId := range company.ChatIds {
msg.Text += strings.Replace(fmt.Sprintf("\n%d", chatId), "-", "\\-", 1)
msg.Text += fmt.Sprintf("\n%d", chatId)
}
}

return msg, nil
}

func replaceSpecialCharacters(str string) string {
for _, char := range []string{"_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"} {
str = strings.Replace(str, char, "\\"+char, -1)
}
return str
}
6 changes: 6 additions & 0 deletions internal/transport/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func (b *TelegramBot) Run() {

msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")

b.logger.Debug("got command",
slog.String("command", update.Message.Command()),
slog.String("text", update.Message.Text),
slog.Int64("chatID", update.Message.Chat.ID),
)

// Extract the command from the Message.
switch update.Message.Command() {
case helpCommand:
Expand Down

0 comments on commit b1a08f1

Please sign in to comment.