- 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)
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){}
for _, handler := range router.Commands {

View File

@ -7,6 +7,7 @@ import (
"ticket-pimp/internal/domain"
"github.com/bwmarrin/discordgo"
"github.com/imroc/req/v3"
)
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
doneButtonIsDisabled bool = false
state domain.TaskState = domain.NewTaskState()
message string
)
// Check what flow was touched: -------------------------------------------------------------------------
@ -137,10 +139,12 @@ func (c *client) handleTaskButton(s *discordgo.Session, i *discordgo.Interaction
opt = 0
doneButtonIsDisabled = false
state = domain.InrpogressTaskState()
message = "взята в работу"
case "task_close":
opt = 1
doneButtonIsDisabled = true
state = domain.DoneTaskState()
message = "выполнена"
}
// 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: -------------------------------------------------------------------
newContent := convertable.
ExtractDomain().
DiscordMessage(state)
task := convertable.
ExtractDomain()
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: ---------------------------------------------
_, 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)
}
}
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
conf *domain.DiscordConfig
tgConf *domain.TelegramConfig
}
// Подключение роутов к 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
r.controller = wc
@ -50,6 +51,7 @@ func InitRouter(wc controller.WorkflowController, conf *domain.DiscordConfig) *c
Moderated: true,
EmojiName: "✅",
})
r.tgConf = tgConf
return &r
}