- send task status to creator in TG

This commit is contained in:
naudachu 2023-11-17 14:24:14 +05:00
parent 2c24b73a82
commit afa3e03a27
3 changed files with 64 additions and 5 deletions

View File

@ -49,7 +49,7 @@ func Run(conf domain.Config, opts DiscordOptions) error {
s := initBotWith(token) s := initBotWith(token)
router := handler.InitRouter(*opts.Controller, &conf.Discord) router := handler.InitRouter(*opts.Controller, &conf.Discord, &conf.Telegram)
commandHandlers := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){} commandHandlers := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){}
for _, handler := range router.Commands { for _, handler := range router.Commands {

View File

@ -7,6 +7,7 @@ import (
"ticket-pimp/internal/domain" "ticket-pimp/internal/domain"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/imroc/req/v3"
) )
func (c *client) setFlag(s *discordgo.Session, i *discordgo.InteractionCreate, tag *discordgo.ForumTag) error { func (c *client) setFlag(s *discordgo.Session, i *discordgo.InteractionCreate, tag *discordgo.ForumTag) error {
@ -129,6 +130,7 @@ func (c *client) handleTaskButton(s *discordgo.Session, i *discordgo.Interaction
opt int = -1 opt int = -1
doneButtonIsDisabled bool = false doneButtonIsDisabled bool = false
state domain.TaskState = domain.NewTaskState() state domain.TaskState = domain.NewTaskState()
message string
) )
// Check what flow was touched: ------------------------------------------------------------------------- // Check what flow was touched: -------------------------------------------------------------------------
@ -137,10 +139,12 @@ func (c *client) handleTaskButton(s *discordgo.Session, i *discordgo.Interaction
opt = 0 opt = 0
doneButtonIsDisabled = false doneButtonIsDisabled = false
state = domain.InrpogressTaskState() state = domain.InrpogressTaskState()
message = "взята в работу"
case "task_close": case "task_close":
opt = 1 opt = 1
doneButtonIsDisabled = true doneButtonIsDisabled = true
state = domain.DoneTaskState() state = domain.DoneTaskState()
message = "выполнена"
} }
// Send the task update to db -------------------------------------------------------------------------- // Send the task update to db --------------------------------------------------------------------------
@ -151,9 +155,18 @@ func (c *client) handleTaskButton(s *discordgo.Session, i *discordgo.Interaction
} }
// Map DB's response to domain.Task: ------------------------------------------------------------------- // Map DB's response to domain.Task: -------------------------------------------------------------------
newContent := convertable. task := convertable.
ExtractDomain(). ExtractDomain()
DiscordMessage(state)
newContent := task.DiscordMessage(state)
// Send message to the creator in Telegram: -------------------------------------------------------------
if task.CreatorLink != "" {
c.sendTelegramMessageToCreator(
task.CreatorLink,
fmt.Sprintf("Task ID: %d %s", task.ID, message))
}
// Send a message to the thread about the task was started: --------------------------------------------- // Send a message to the thread about the task was started: ---------------------------------------------
_, err = s.ChannelMessageSendComplex(i.ChannelID, &discordgo.MessageSend{ _, err = s.ChannelMessageSendComplex(i.ChannelID, &discordgo.MessageSend{
@ -190,3 +203,47 @@ func (c *client) handleTaskButton(s *discordgo.Session, i *discordgo.Interaction
log.Printf("error while `start` tag setting: %v", err) log.Printf("error while `start` tag setting: %v", err)
} }
} }
type TelegramMessage struct {
ChatID string `json:"chat_id"`
Text string `json:"text"`
DisableNotification bool `json:"disable_notification"`
ParseMode string `json:"parse_mode"`
DisablePreview bool `json:"disable_web_page_preview"`
}
func (c *client) sendTelegramMessageToCreator(tgChatID string, text string) {
http := req.C()
http.R().
SetBody(&TelegramMessage{
ChatID: tgChatID,
Text: text,
DisableNotification: true,
ParseMode: "HTML",
DisablePreview: true,
}).
Post("https://api.telegram.org/bot" + c.tgConf.Token + "/sendMessage")
// [HTTP Kit Marlerino]::POST(
// "https://api.telegram.org/bot" + Config.botToken +
// "/sendMessage",
// "",
// Object(
// "chat_id",
// thisRow.[Creator ID].ToText(),
// "text",
// Format(
// "<a href='{1}'>{2}</a> взята в работу",
// thisRow.ObjectLink().ToText(),
// "Задача"
// ),
// "disable_notification",
// true,
// "parse_mode",
// "HTML",
// "disable_web_page_preview",
// true
// )
// )
}

View File

@ -16,10 +16,11 @@ type client struct {
controller controller.WorkflowController controller controller.WorkflowController
conf *domain.DiscordConfig conf *domain.DiscordConfig
tgConf *domain.TelegramConfig
} }
// Подключение роутов к Discord боту // Подключение роутов к Discord боту
func InitRouter(wc controller.WorkflowController, conf *domain.DiscordConfig) *client { func InitRouter(wc controller.WorkflowController, conf *domain.DiscordConfig, tgConf *domain.TelegramConfig) *client {
var r client var r client
r.controller = wc r.controller = wc
@ -50,6 +51,7 @@ func InitRouter(wc controller.WorkflowController, conf *domain.DiscordConfig) *c
Moderated: true, Moderated: true,
EmojiName: "✅", EmojiName: "✅",
}) })
r.tgConf = tgConf
return &r return &r
} }