Skip to content

Commit

Permalink
add separate commands for setting target chat id for a group and for …
Browse files Browse the repository at this point in the history
…a private chat
  • Loading branch information
akshettrj committed Jan 13, 2023
1 parent 18c488f commit 5e0c516
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions telegram/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func AddTelegramHandlers() {
handlers.NewCommand("clearpairhistory", ClearMessageIdPairsHistoryHandler),
handlers.NewCommand("restartwa", RestartWhatsAppConnectionHandler),
handlers.NewCommand("joininvitelink", JoinInviteLinkHandler),
handlers.NewCommand("settargetchat", SetTargetChatHandler),
handlers.NewCommand("settargetgroupchat", SetTargetGroupChatHandler),
handlers.NewCommand("settargetprivatechat", SetTargetPrivateChatHandler),
handlers.NewCommand("send", SendToWhatsAppHandler),
handlers.NewCommand("help", HelpCommandHandler),
)
Expand Down Expand Up @@ -73,8 +74,12 @@ func AddTelegramHandlers() {
Description: "Join a WhatsApp chat using invite link",
},
gotgbot.BotCommand{
Command: "settargetchat",
Description: "Set the target WhatsApp chat for current thread",
Command: "settargetgroupchat",
Description: "Set the target WhatsApp group chat for current thread",
},
gotgbot.BotCommand{
Command: "settargetprivatechat",
Description: "Set the target WhatsApp private chat for current thread",
},
gotgbot.BotCommand{
Command: "send",
Expand Down Expand Up @@ -289,12 +294,12 @@ func JoinInviteLinkHandler(b *gotgbot.Bot, c *ext.Context) error {
fmt.Sprintf("Joined a new group with ID: <code>%s</code>", groupID.String()))
}

func SetTargetChatHandler(b *gotgbot.Bot, c *ext.Context) error {
func SetTargetGroupChatHandler(b *gotgbot.Bot, c *ext.Context) error {
if !utils.TgUpdateIsAuthorized(b, c) {
return nil
}

usageString := "Usage: (Send in a topic) <code>" + html.EscapeString("/settargetchat <group_id>") + "</code>"
usageString := "Usage: (Send in a topic) <code>" + html.EscapeString("/settargetgroupchat <group_id>") + "</code>"

args := c.Args()
if len(args) <= 1 {
Expand Down Expand Up @@ -333,6 +338,44 @@ func SetTargetChatHandler(b *gotgbot.Bot, c *ext.Context) error {
return utils.TgReplyTextByContext(b, c, "Successfully mapped")
}

func SetTargetPrivateChatHandler(b *gotgbot.Bot, c *ext.Context) error {
if !utils.TgUpdateIsAuthorized(b, c) {
return nil
}

usageString := "Usage: (Send in a topic) <code>" + html.EscapeString("/settargetprivatechat <user_id>") + "</code>"

args := c.Args()
if len(args) <= 1 {
return utils.TgReplyTextByContext(b, c, usageString)
}

if !c.EffectiveMessage.IsTopicMessage || c.EffectiveMessage.MessageThreadId == 0 {
return utils.TgReplyTextByContext(b, c, "The command should be sent in a topic")
}

var (
cfg = state.State.Config
groupID = args[1]
)

userJID, _ := utils.WaParseJID(groupID)

_, threadFound, err := database.ChatThreadGetTgFromWa(userJID.String(), cfg.Telegram.TargetChatID)
if err != nil {
return utils.TgReplyWithErrorByContext(b, c, "Failed to check database for existing mapping", err)
} else if threadFound {
return utils.TgReplyTextByContext(b, c, "A topic already exists in database for the given WhatsApp chat. Aborting...")
}

err = database.ChatThreadAddNewPair(userJID.String(), cfg.Telegram.TargetChatID, c.EffectiveMessage.MessageThreadId)
if err != nil {
return utils.TgReplyWithErrorByContext(b, c, "Failed to add the mapping in database. Unsuccessful", err)
}

return utils.TgReplyTextByContext(b, c, "Successfully mapped")
}

func HelpCommandHandler(b *gotgbot.Bot, c *ext.Context) error {
if !utils.TgUpdateIsAuthorized(b, c) {
return nil
Expand All @@ -348,7 +391,6 @@ func HelpCommandHandler(b *gotgbot.Bot, c *ext.Context) error {
return utils.TgReplyTextByContext(b, c, helpString)
}

// TODO : Complete the function
func SendToWhatsAppHandler(b *gotgbot.Bot, c *ext.Context) error {
if !utils.TgUpdateIsAuthorized(b, c) {
return nil
Expand Down

0 comments on commit 5e0c516

Please sign in to comment.