From a52b252c859d213836adf2b763596f7a0d589b25 Mon Sep 17 00:00:00 2001 From: Divanshu Chauhan Date: Mon, 5 Aug 2024 12:22:40 +0530 Subject: [PATCH] fix welcome --- alita/utils/helpers/helpers.go | 79 ++++++++++++---------------------- 1 file changed, 28 insertions(+), 51 deletions(-) diff --git a/alita/utils/helpers/helpers.go b/alita/utils/helpers/helpers.go index 4c22a8e4..8d3ad1e8 100644 --- a/alita/utils/helpers/helpers.go +++ b/alita/utils/helpers/helpers.go @@ -670,30 +670,17 @@ func GetWelcomeType(msg *gotgbot.Message, greetingType string) (text string, dat errorMsg = fmt.Sprintf("You need to give me some content to %s users!", greetingType) var ( rawText string - args []string + args = strings.Fields(msg.Text)[1:] ) _buttons := make([]tgmd2html.ButtonV2, 0) replyMsg := msg.ReplyToMessage - // Split msg.Text into fields and ensure there are enough fields - fields := strings.Fields(msg.Text) - if len(fields) > 1 { - args = fields[1:] - } else { - log.Printf("Message text has insufficient fields: %v", fields) - } - // set rawText from helper function setRawText(msg, args, &rawText) if len(args) >= 1 && msg.ReplyToMessage == nil { fileid = "" - splitRawText := strings.SplitN(rawText, " ", 2) - if len(splitRawText) > 1 { - text, _buttons = tgmd2html.MD2HTMLButtonsV2(splitRawText[1]) - } else { - text, _buttons = tgmd2html.MD2HTMLButtonsV2("") - } + text, _buttons = tgmd2html.MD2HTMLButtonsV2(rawText) dataType = db.TEXT } else if msg.ReplyToMessage != nil { if replyMsg.ReplyMarkup == nil { @@ -1023,21 +1010,32 @@ var NotesEnumFuncMap = map[int]func(b *gotgbot.Bot, ctx *ext.Context, noteData * // GreetingsEnumFuncMap FIXME: when using /welcome command in private with connection, the string of welcome is sent to connected chat instead of pm // GreetingsEnumFuncMap // A rather very complicated GreetingsEnumFuncMap Variable made by me to send filters in an appropriate way -var GreetingsEnumFuncMap = map[int]func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup) (*gotgbot.Message, error){ - db.TEXT: func(b *gotgbot.Bot, ctx *ext.Context, msg, _ string, keyb *gotgbot.InlineKeyboardMarkup) (*gotgbot.Message, error) { +var GreetingsEnumFuncMap = map[int]func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup, replyToMsgId, customChatId int64) (*gotgbot.Message, error){ + db.TEXT: func(b *gotgbot.Bot, ctx *ext.Context, msg, _ string, keyb *gotgbot.InlineKeyboardMarkup, replyToMsgId, customChatId int64) (*gotgbot.Message, error) { + // if customChatId is not 0, then send the message to current chat + if customChatId != 0 { + customChatId = ctx.EffectiveChat.Id + } + replyPara := &gotgbot.ReplyParameters{} + if replyToMsgId != 0 { + replyPara = &gotgbot.ReplyParameters{ + MessageId: replyToMsgId, + } + } return b.SendMessage( - ctx.EffectiveChat.Id, + customChatId, msg, &gotgbot.SendMessageOpts{ ParseMode: HTML, LinkPreviewOptions: &gotgbot.LinkPreviewOptions{ IsDisabled: true, }, - ReplyMarkup: keyb, + ReplyParameters: replyPara, + ReplyMarkup: keyb, }, ) }, - db.STICKER: func(b *gotgbot.Bot, ctx *ext.Context, _, fileID string, keyb *gotgbot.InlineKeyboardMarkup) (*gotgbot.Message, error) { + db.STICKER: func(b *gotgbot.Bot, ctx *ext.Context, _, fileID string, keyb *gotgbot.InlineKeyboardMarkup, replyToMsgId, customChatId int64) (*gotgbot.Message, error) { return b.SendSticker( ctx.EffectiveChat.Id, gotgbot.InputFileByID(fileID), @@ -1047,7 +1045,7 @@ var GreetingsEnumFuncMap = map[int]func(b *gotgbot.Bot, ctx *ext.Context, msg, f }, ) }, - db.DOCUMENT: func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup) (*gotgbot.Message, error) { + db.DOCUMENT: func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup, replyToMsgId, customChatId int64) (*gotgbot.Message, error) { return b.SendDocument( ctx.EffectiveChat.Id, gotgbot.InputFileByID(fileID), @@ -1059,7 +1057,7 @@ var GreetingsEnumFuncMap = map[int]func(b *gotgbot.Bot, ctx *ext.Context, msg, f }, ) }, - db.PHOTO: func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup) (*gotgbot.Message, error) { + db.PHOTO: func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup, replyToMsgId, customChatId int64) (*gotgbot.Message, error) { return b.SendPhoto( ctx.EffectiveChat.Id, gotgbot.InputFileByID(fileID), @@ -1071,7 +1069,7 @@ var GreetingsEnumFuncMap = map[int]func(b *gotgbot.Bot, ctx *ext.Context, msg, f }, ) }, - db.AUDIO: func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup) (*gotgbot.Message, error) { + db.AUDIO: func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup, replyToMsgId, customChatId int64) (*gotgbot.Message, error) { return b.SendAudio( ctx.EffectiveChat.Id, gotgbot.InputFileByID(fileID), @@ -1083,7 +1081,7 @@ var GreetingsEnumFuncMap = map[int]func(b *gotgbot.Bot, ctx *ext.Context, msg, f }, ) }, - db.VOICE: func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup) (*gotgbot.Message, error) { + db.VOICE: func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup, replyToMsgId, customChatId int64) (*gotgbot.Message, error) { return b.SendVoice( ctx.EffectiveChat.Id, gotgbot.InputFileByID(fileID), @@ -1095,7 +1093,7 @@ var GreetingsEnumFuncMap = map[int]func(b *gotgbot.Bot, ctx *ext.Context, msg, f }, ) }, - db.VIDEO: func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup) (*gotgbot.Message, error) { + db.VIDEO: func(b *gotgbot.Bot, ctx *ext.Context, msg, fileID string, keyb *gotgbot.InlineKeyboardMarkup, replyToMsgId, customChatId int64) (*gotgbot.Message, error) { return b.SendVideo( ctx.EffectiveChat.Id, gotgbot.InputFileByID(fileID), @@ -1107,7 +1105,7 @@ var GreetingsEnumFuncMap = map[int]func(b *gotgbot.Bot, ctx *ext.Context, msg, f }, ) }, - db.VideoNote: func(b *gotgbot.Bot, ctx *ext.Context, _, fileID string, keyb *gotgbot.InlineKeyboardMarkup) (*gotgbot.Message, error) { + db.VideoNote: func(b *gotgbot.Bot, ctx *ext.Context, _, fileID string, keyb *gotgbot.InlineKeyboardMarkup, replyToMsgId, customChatId int64) (*gotgbot.Message, error) { return b.SendVideoNote( ctx.EffectiveChat.Id, gotgbot.InputFileByID(fileID), @@ -1315,43 +1313,22 @@ func preFixes(buttons []tgmd2html.ButtonV2, defaultNameButton string, text *stri } } -// function used to get rawText from gotgbot.Message +// function used to get rawtext from gotgbot.Message func setRawText(msg *gotgbot.Message, args []string, rawText *string) { replyMsg := msg.ReplyToMessage - if replyMsg == nil { if msg.Text == "" && msg.Caption != "" { - splitText := strings.SplitN(msg.OriginalCaptionMDV2(), " ", 2) - if len(splitText) > 1 { - *rawText = splitText[1] // using [1] to remove the command - } else { - log.Println("Insufficient split elements for OriginalCaptionMDV2") - *rawText = "" - } + *rawText = strings.SplitN(msg.OriginalCaptionMDV2(), " ", 2)[1] // using [1] to remove the command } else if msg.Text != "" && msg.Caption == "" { - splitText := strings.SplitN(msg.OriginalMDV2(), " ", 2) - if len(splitText) > 1 { - *rawText = splitText[1] // using [1] to remove the command - } else { - log.Println("Insufficient split elements for OriginalMDV2") - *rawText = "" - } + *rawText = strings.SplitN(msg.OriginalMDV2(), " ", 2)[1] // using [1] to remove the command } } else { if replyMsg.Text == "" && replyMsg.Caption != "" { *rawText = replyMsg.OriginalCaptionMDV2() } else if replyMsg.Caption == "" && len(args) >= 2 { - splitText := strings.SplitN(msg.OriginalMDV2(), " ", 3) - if len(splitText) > 2 { - *rawText = splitText[2] // using [2] to remove the command - } else { - log.Println("Insufficient split elements for OriginalMDV2 with args") - *rawText = "" - } + *rawText = strings.SplitN(msg.OriginalMDV2(), " ", 3)[2] // using [1] to remove the command } else { *rawText = replyMsg.OriginalMDV2() } } - - log.Printf("setRawText: msg.Text = %s, args = %v, rawText = %s\n", msg.Text, args, *rawText) }