|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gotd/td/telegram/message/markup" |
| 5 | + "github.com/gotd/td/tg" |
| 6 | +) |
| 7 | + |
| 8 | +// replyMarkupToTg translates a Bot API ReplyMarkup into the MTProto |
| 9 | +// tg.ReplyMarkupClass understood by the sender. |
| 10 | +// |
| 11 | +// The switch over the sealed ReplyMarkup union is exhaustive (enforced by the |
| 12 | +// gochecksumtype linter): every variant is handled. |
| 13 | +func replyMarkupToTg(m ReplyMarkup) (tg.ReplyMarkupClass, error) { |
| 14 | + switch m := m.(type) { |
| 15 | + case *InlineKeyboardMarkup: |
| 16 | + rows := make([]tg.KeyboardButtonRow, 0, len(m.InlineKeyboard)) |
| 17 | + for _, row := range m.InlineKeyboard { |
| 18 | + buttons := make([]tg.KeyboardButtonClass, len(row)) |
| 19 | + for i, btn := range row { |
| 20 | + b, err := inlineButtonToTg(btn) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + buttons[i] = b |
| 25 | + } |
| 26 | + rows = append(rows, tg.KeyboardButtonRow{Buttons: buttons}) |
| 27 | + } |
| 28 | + return &tg.ReplyInlineMarkup{Rows: rows}, nil |
| 29 | + case *ReplyKeyboardMarkup: |
| 30 | + rows := make([]tg.KeyboardButtonRow, 0, len(m.Keyboard)) |
| 31 | + for _, row := range m.Keyboard { |
| 32 | + buttons := make([]tg.KeyboardButtonClass, len(row)) |
| 33 | + for i, btn := range row { |
| 34 | + buttons[i] = keyboardButtonToTg(btn) |
| 35 | + } |
| 36 | + rows = append(rows, tg.KeyboardButtonRow{Buttons: buttons}) |
| 37 | + } |
| 38 | + res := &tg.ReplyKeyboardMarkup{ |
| 39 | + Resize: m.ResizeKeyboard, |
| 40 | + SingleUse: m.OneTimeKeyboard, |
| 41 | + Selective: m.Selective, |
| 42 | + Persistent: m.IsPersistent, |
| 43 | + Rows: rows, |
| 44 | + } |
| 45 | + if m.InputFieldPlaceholder != "" { |
| 46 | + res.SetPlaceholder(m.InputFieldPlaceholder) |
| 47 | + } |
| 48 | + return res, nil |
| 49 | + case *ReplyKeyboardRemove: |
| 50 | + if m.Selective { |
| 51 | + return markup.SelectiveHide(), nil |
| 52 | + } |
| 53 | + return markup.Hide(), nil |
| 54 | + case *ForceReply: |
| 55 | + res := &tg.ReplyKeyboardForceReply{Selective: m.Selective} |
| 56 | + if m.InputFieldPlaceholder != "" { |
| 57 | + res.SetPlaceholder(m.InputFieldPlaceholder) |
| 58 | + } |
| 59 | + return res, nil |
| 60 | + default: |
| 61 | + return nil, &Error{Code: 400, Description: "Bad Request: unsupported reply markup"} |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func inlineButtonToTg(btn InlineKeyboardButton) (tg.KeyboardButtonClass, error) { |
| 66 | + switch { |
| 67 | + case btn.URL != "": |
| 68 | + return markup.URL(btn.Text, btn.URL), nil |
| 69 | + case btn.CallbackData != "": |
| 70 | + return markup.Callback(btn.Text, []byte(btn.CallbackData)), nil |
| 71 | + case btn.WebApp != nil: |
| 72 | + return markup.WebView(btn.Text, btn.WebApp.URL), nil |
| 73 | + case btn.SwitchInlineQuery != nil: |
| 74 | + return markup.SwitchInline(btn.Text, *btn.SwitchInlineQuery, false), nil |
| 75 | + case btn.SwitchInlineQueryCurrentChat != nil: |
| 76 | + return markup.SwitchInline(btn.Text, *btn.SwitchInlineQueryCurrentChat, true), nil |
| 77 | + case btn.Pay: |
| 78 | + return markup.Buy(btn.Text), nil |
| 79 | + default: |
| 80 | + return nil, &Error{Code: 400, Description: "Bad Request: text buttons are unallowed in the inline keyboard"} |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func keyboardButtonToTg(btn KeyboardButton) tg.KeyboardButtonClass { |
| 85 | + switch { |
| 86 | + case btn.RequestContact: |
| 87 | + return markup.RequestPhone(btn.Text) |
| 88 | + case btn.RequestLocation: |
| 89 | + return markup.RequestGeoLocation(btn.Text) |
| 90 | + case btn.RequestPoll != nil: |
| 91 | + return markup.RequestPoll(btn.Text, btn.RequestPoll.Type == PollQuiz) |
| 92 | + case btn.WebApp != nil: |
| 93 | + return markup.SimpleWebView(btn.Text, btn.WebApp.URL) |
| 94 | + default: |
| 95 | + return markup.Button(btn.Text) |
| 96 | + } |
| 97 | +} |
0 commit comments