Skip to content

Commit

Permalink
add command to unlink the current tg thread
Browse files Browse the repository at this point in the history
  • Loading branch information
akshettrj committed Oct 8, 2023
1 parent 23d6dee commit 7974355
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions database/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ func ChatThreadGetTgFromWa(waChatId string, tgChatId int64) (int64, bool, error)
return chatPair.TgThreadId, found, res.Error
}

func ChatThreadDropPairByTg(tgChatId, tgThreadId int64) error {

db := state.State.Database

res := db.Where("tg_chat_id = ? AND tg_thread_id = ?", tgChatId, tgThreadId).Delete(&ChatThreadPair{})

return res.Error
}

func ChatThreadGetWaFromTg(tgChatId, tgThreadId int64) (string, error) {

db := state.State.Database
Expand Down
40 changes: 39 additions & 1 deletion telegram/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func AddTelegramHandlers() {
handlers.NewCommand("settargetprivatechat", SetTargetPrivateChatHandler),
"Set the target WhatsApp private chat for current thread",
},
waTgBridgeCommand{
handlers.NewCommand("unlinkthread", UnlinkThreadHandler),
"Unlink the current thread from its WhatsApp chat",
},
waTgBridgeCommand{
handlers.NewCommand("getprofilepicture", GetProfilePictureHandler),
"Get the profile picture of user or group using its ID",
Expand Down Expand Up @@ -480,12 +484,46 @@ func SetTargetGroupChatHandler(b *gotgbot.Bot, c *ext.Context) error {
return err
}

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

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

var (
tgChatId = c.EffectiveChat.Id
tgThreadId = c.EffectiveMessage.MessageThreadId
)

waChatId, err := database.ChatThreadGetWaFromTg(tgChatId, tgThreadId)
if err != nil {
err = utils.TgReplyWithErrorByContext(b, c, "Failed to get existing chat ID pairing", err)
return err
} else if waChatId == "" {
_, err := utils.TgReplyTextByContext(b, c, "No existing chat pairing found!!", nil)
return err
}

err = database.ChatThreadDropPairByTg(tgChatId, tgThreadId)
if err != nil {
err = utils.TgReplyWithErrorByContext(b, c, "Failed to delete the thread chat pairing", err)
return err
}

_, err = utils.TgReplyTextByContext(b, c, "Successfully unlinked", nil)
return err
}

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>"
usageString := "Usage (Send in a topic): <code>" + html.EscapeString("/settargetprivatechat <user_id>") + "</code>"

args := c.Args()
if len(args) <= 1 {
Expand Down

0 comments on commit 7974355

Please sign in to comment.