All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 5m55s
实现联系信息提交功能,包括: 1. 新增ContactRequest类型定义 2. 添加POST /contact路由 3. 实现联系信息提交处理逻辑 4. 通过Telegram发送联系信息通知 5. 在Telegram配置中添加GroupChatID字段
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package common
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"strconv"
|
||
"strings"
|
||
|
||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||
"github.com/perfect-panel/server/internal/svc"
|
||
"github.com/perfect-panel/server/internal/types"
|
||
"github.com/perfect-panel/server/pkg/logger"
|
||
"github.com/perfect-panel/server/pkg/xerr"
|
||
"github.com/pkg/errors"
|
||
)
|
||
|
||
type ContactLogic struct {
|
||
logger.Logger
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
}
|
||
|
||
func NewContactLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ContactLogic {
|
||
return &ContactLogic{
|
||
Logger: logger.WithContext(ctx),
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
}
|
||
}
|
||
|
||
func (l *ContactLogic) SubmitContact(req *types.ContactRequest) error {
|
||
if l.svcCtx.TelegramBot == nil {
|
||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "telegram bot not initialized")
|
||
}
|
||
chatIDStr := l.svcCtx.Config.Telegram.GroupChatID
|
||
if chatIDStr == "" {
|
||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "telegram group chat id not configured")
|
||
}
|
||
chatID, err := strconv.ParseInt(chatIDStr, 10, 64)
|
||
if err != nil {
|
||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "invalid group chat id: %v", err.Error())
|
||
}
|
||
|
||
name := escapeMarkdown(req.Name)
|
||
email := escapeMarkdown(req.Email)
|
||
other := req.OtherContact
|
||
if strings.TrimSpace(other) == "" {
|
||
other = "无"
|
||
}
|
||
other = escapeMarkdown(other)
|
||
notes := req.Notes
|
||
if strings.TrimSpace(notes) == "" {
|
||
notes = "无"
|
||
}
|
||
notes = escapeMarkdown(notes)
|
||
|
||
text := fmt.Sprintf("新的联系/合作信息\n称呼:%s\n邮箱:%s\n其他联系方式:%s\n优势/备注:%s", name, email, other, notes)
|
||
msg := tgbotapi.NewMessage(chatID, text)
|
||
msg.ParseMode = "markdown"
|
||
_, err = l.svcCtx.TelegramBot.Send(msg)
|
||
if err != nil {
|
||
l.Errorw("send telegram message failed", logger.Field("error", err.Error()))
|
||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "send telegram message failed: %v", err.Error())
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func escapeMarkdown(s string) string {
|
||
return strings.ReplaceAll(s, "_", "\\_")
|
||
}
|