diff --git a/examples/echo-bot/main.go b/examples/echo-bot/main.go index 6fe17ef..99291b6 100644 --- a/examples/echo-bot/main.go +++ b/examples/echo-bot/main.go @@ -89,7 +89,7 @@ func newBot() *tgb.Bot { }, tgb.Command("start", tgb.WithCommandAlias("help"))). // handles gopher image Message(func(ctx context.Context, msg *tgb.MessageUpdate) error { - if err := msg.Update.Respond(ctx, msg.AnswerChatAction("upload_photo")); err != nil { + if err := msg.Update.Respond(ctx, msg.AnswerChatAction(tg.ChatActionUploadPhoto)); err != nil { return fmt.Errorf("answer chat action: %w", err) } diff --git a/examples/quote-bot/main.go b/examples/quote-bot/main.go index 76eb1ee..854ece6 100644 --- a/examples/quote-bot/main.go +++ b/examples/quote-bot/main.go @@ -104,7 +104,7 @@ func run(ctx context.Context) error { Description: quoteText(quote.Text), InputMessageContent: tg.InputTextMessageContent{ MessageText: messageText, - ParseMode: tg.HTML.Name(), + ParseMode: tg.HTML.String(), }, ReplyMarkup: tg.NewInlineKeyboardMarkup( tg.NewButtonRow( diff --git a/methods_gen.go b/methods_gen.go index 06c7661..818d502 100644 --- a/methods_gen.go +++ b/methods_gen.go @@ -296,7 +296,7 @@ func (call *SendMessageCall) Text(text string) *SendMessageCall { // ParseMode Mode for parsing entities in the message text. See formatting options for more details. func (call *SendMessageCall) ParseMode(parseMode ParseMode) *SendMessageCall { - call.request.ParseMode("parse_mode", parseMode) + call.request.Stringer("parse_mode", parseMode) return call } @@ -461,7 +461,7 @@ func (call *CopyMessageCall) Caption(caption string) *CopyMessageCall { // ParseMode Mode for parsing entities in the new caption. See formatting options for more details. func (call *CopyMessageCall) ParseMode(parseMode ParseMode) *CopyMessageCall { - call.request.ParseMode("parse_mode", parseMode) + call.request.Stringer("parse_mode", parseMode) return call } @@ -549,7 +549,7 @@ func (call *SendPhotoCall) Caption(caption string) *SendPhotoCall { // ParseMode Mode for parsing entities in the photo caption. See formatting options for more details. func (call *SendPhotoCall) ParseMode(parseMode ParseMode) *SendPhotoCall { - call.request.ParseMode("parse_mode", parseMode) + call.request.Stringer("parse_mode", parseMode) return call } @@ -640,7 +640,7 @@ func (call *SendAudioCall) Caption(caption string) *SendAudioCall { // ParseMode Mode for parsing entities in the audio caption. See formatting options for more details. func (call *SendAudioCall) ParseMode(parseMode ParseMode) *SendAudioCall { - call.request.ParseMode("parse_mode", parseMode) + call.request.Stringer("parse_mode", parseMode) return call } @@ -759,7 +759,7 @@ func (call *SendDocumentCall) Caption(caption string) *SendDocumentCall { // ParseMode Mode for parsing entities in the document caption. See formatting options for more details. func (call *SendDocumentCall) ParseMode(parseMode ParseMode) *SendDocumentCall { - call.request.ParseMode("parse_mode", parseMode) + call.request.Stringer("parse_mode", parseMode) return call } @@ -878,7 +878,7 @@ func (call *SendVideoCall) Caption(caption string) *SendVideoCall { // ParseMode Mode for parsing entities in the video caption. See formatting options for more details. func (call *SendVideoCall) ParseMode(parseMode ParseMode) *SendVideoCall { - call.request.ParseMode("parse_mode", parseMode) + call.request.Stringer("parse_mode", parseMode) return call } @@ -997,7 +997,7 @@ func (call *SendAnimationCall) Caption(caption string) *SendAnimationCall { // ParseMode Mode for parsing entities in the animation caption. See formatting options for more details. func (call *SendAnimationCall) ParseMode(parseMode ParseMode) *SendAnimationCall { - call.request.ParseMode("parse_mode", parseMode) + call.request.Stringer("parse_mode", parseMode) return call } @@ -1087,7 +1087,7 @@ func (call *SendVoiceCall) Caption(caption string) *SendVoiceCall { // ParseMode Mode for parsing entities in the voice message caption. See formatting options for more details. func (call *SendVoiceCall) ParseMode(parseMode ParseMode) *SendVoiceCall { - call.request.ParseMode("parse_mode", parseMode) + call.request.Stringer("parse_mode", parseMode) return call } @@ -1925,18 +1925,18 @@ type SendChatActionCall struct { // NewSendChatActionCall constructs a new SendChatActionCall with required parameters. // chatId - Unique identifier for the target chat or username of the target channel (in the format @channelusername) // action - Type of action to broadcast. Choose one, depending on what the user is about to receive: typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_voice or upload_voice for voice notes, upload_document for general files, choose_sticker for stickers, find_location for location data, record_video_note or upload_video_note for video notes. -func NewSendChatActionCall(chatId PeerID, action string) *SendChatActionCall { +func NewSendChatActionCall(chatId PeerID, action ChatAction) *SendChatActionCall { return &SendChatActionCall{ CallNoResult{ request: NewRequest("sendChatAction"). PeerID("chat_id", chatId). - String("action", action), + Stringer("action", action), }, } } // SendChatActionCall constructs a new SendChatActionCall with required parameters. -func (client *Client) SendChatAction(chatId PeerID, action string) *SendChatActionCall { +func (client *Client) SendChatAction(chatId PeerID, action ChatAction) *SendChatActionCall { return callWithClient( client, NewSendChatActionCall(chatId, action), @@ -1950,8 +1950,8 @@ func (call *SendChatActionCall) ChatId(chatId PeerID) *SendChatActionCall { } // Action Type of action to broadcast. Choose one, depending on what the user is about to receive: typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_voice or upload_voice for voice notes, upload_document for general files, choose_sticker for stickers, find_location for location data, record_video_note or upload_video_note for video notes. -func (call *SendChatActionCall) Action(action string) *SendChatActionCall { - call.request.String("action", action) +func (call *SendChatActionCall) Action(action ChatAction) *SendChatActionCall { + call.request.Stringer("action", action) return call } @@ -3642,7 +3642,7 @@ func (call *EditMessageTextCall) Text(text string) *EditMessageTextCall { // ParseMode Mode for parsing entities in the message text. See formatting options for more details. func (call *EditMessageTextCall) ParseMode(parseMode ParseMode) *EditMessageTextCall { - call.request.ParseMode("parse_mode", parseMode) + call.request.Stringer("parse_mode", parseMode) return call } @@ -3741,7 +3741,7 @@ func (call *EditMessageCaptionCall) Caption(caption string) *EditMessageCaptionC // ParseMode Mode for parsing entities in the message caption. See formatting options for more details. func (call *EditMessageCaptionCall) ParseMode(parseMode ParseMode) *EditMessageCaptionCall { - call.request.ParseMode("parse_mode", parseMode) + call.request.Stringer("parse_mode", parseMode) return call } diff --git a/parse_mode.go b/parse_mode.go index f76786d..f740da5 100644 --- a/parse_mode.go +++ b/parse_mode.go @@ -7,7 +7,7 @@ import ( ) type ParseMode interface { - Name() string + String() string // Change separator for next calls Sep(v string) ParseMode @@ -140,7 +140,7 @@ func (pm parseMode) Line(v ...string) string { return strings.Join(v, " ") } -func (pm parseMode) Name() string { +func (pm parseMode) String() string { return pm.name } diff --git a/parse_mode_test.go b/parse_mode_test.go index a215458..f8191a3 100644 --- a/parse_mode_test.go +++ b/parse_mode_test.go @@ -7,7 +7,7 @@ import ( ) func TestParseModeHTML(t *testing.T) { - assert.Equal(t, "HTML", HTML.Name()) + assert.Equal(t, "HTML", HTML.String()) assert.Equal(t, "Hello World", HTML.Line("Hello", "World")) assert.Equal(t, "Hello\nWorld", HTML.Text("Hello", "World")) assert.Equal(t, "Hello World", HTML.Bold("Hello", "World")) @@ -23,7 +23,7 @@ func TestParseModeHTML(t *testing.T) { } func TestParseModeMarkdown(t *testing.T) { - assert.Equal(t, "Markdown", MD.Name()) + assert.Equal(t, "Markdown", MD.String()) assert.Equal(t, "Hello World", MD.Line("Hello", "World")) assert.Equal(t, "Hello\nWorld", MD.Text("Hello", "World")) assert.Equal(t, "*Hello World*", MD.Bold("Hello", "World")) @@ -39,7 +39,7 @@ func TestParseModeMarkdown(t *testing.T) { } func TestParseModeMarkdownV2(t *testing.T) { - assert.Equal(t, "MarkdownV2", MD2.Name()) + assert.Equal(t, "MarkdownV2", MD2.String()) assert.Equal(t, "Hello World", MD2.Line("Hello", "World")) assert.Equal(t, "Hello\nWorld", MD2.Text("Hello", "World")) diff --git a/request.go b/request.go index c135bf9..c842677 100644 --- a/request.go +++ b/request.go @@ -67,10 +67,6 @@ func (r *Request) ChatID(name string, v ChatID) *Request { return r.Int64(name, int64(v)) } -func (r *Request) ParseMode(name string, v ParseMode) *Request { - return r.String(name, v.Name()) -} - func (r *Request) File(name string, arg FileArg) *Request { if arg.FileID != "" { return r.String(name, string(arg.FileID)) @@ -79,6 +75,10 @@ func (r *Request) File(name string, arg FileArg) *Request { } } +func (r *Request) Stringer(name string, v fmt.Stringer) *Request { + return r.String(name, v.String()) +} + // Encode request using encoder. func (r *Request) Encode(encoder Encoder) error { diff --git a/request_test.go b/request_test.go index 5a7699a..c635d29 100644 --- a/request_test.go +++ b/request_test.go @@ -26,7 +26,7 @@ func TestRequestSetters(t *testing.T) { r.Int64("int64", 1) r.Float64("float64", 1) r.ChatID("chat", ChatID(1)) - r.ParseMode("parse_mode", MD2) + r.Stringer("parse_mode", MD2) r.File("file_by_id", FileArg{ FileID: "file_id", }) diff --git a/tgb/update.go b/tgb/update.go index 74409fa..c731141 100644 --- a/tgb/update.go +++ b/tgb/update.go @@ -117,7 +117,7 @@ func (msg *MessageUpdate) AnswerDice(emoji string) *tg.SendDiceCall { } // AnswerChatAction calls sendChatAction with pre-defined chatID to incoming message chat. -func (msg *MessageUpdate) AnswerChatAction(action string) *tg.SendChatActionCall { +func (msg *MessageUpdate) AnswerChatAction(action tg.ChatAction) *tg.SendChatActionCall { return msg.Client.SendChatAction(msg.Chat, action) } diff --git a/tgb/update_test.go b/tgb/update_test.go index 1fc5083..14b1fe6 100644 --- a/tgb/update_test.go +++ b/tgb/update_test.go @@ -270,7 +270,7 @@ func TestMessageUpdateHelpers(t *testing.T) { }, { Name: "AnswerChatAction", - Request: msg.AnswerChatAction("upload_photo").Request(), + Request: msg.AnswerChatAction(tg.ChatActionUploadPhoto).Request(), ExceptedMethod: "sendChatAction", ExpectedArgs: map[string]string{ "chat_id": "123", diff --git a/types_gen_ext.go b/types_gen_ext.go index 944252b..f408c69 100644 --- a/types_gen_ext.go +++ b/types_gen_ext.go @@ -66,6 +66,42 @@ func (chatType *ChatType) UnmarshalJSON(b []byte) error { return nil } +// ChatAction type of action to broadcast via sendChatAction. +type ChatAction int8 + +const ( + ChatActionTyping ChatAction = iota + 1 + ChatActionUploadPhoto + ChatActionRecordVideo + ChatActionUploadVideo + ChatActionRecordVoice + ChatActionUploadVoice + ChatActionUploadDocument + ChatActionChooseSticker + ChatActionFindLocation + ChatActionRecordVideoNote + ChatActionUploadVideoNote +) + +func (action ChatAction) String() string { + if action < ChatActionTyping || action > ChatActionUploadVideoNote { + return "unknown" + } + return [...]string{ + "typing", + "upload_photo", + "record_video", + "upload_video", + "record_voice", + "upload_voice", + "upload_document", + "choose_sticker", + "find_location", + "record_video_note", + "upload_video_note", + }[action-1] +} + // UserID it's unique identifier for Telegram user or bot. type UserID int64 diff --git a/types_gen_ext_test.go b/types_gen_ext_test.go index 50b6f56..a06b65f 100644 --- a/types_gen_ext_test.go +++ b/types_gen_ext_test.go @@ -20,6 +20,10 @@ func TestPeerIDImpl(t *testing.T) { } } +func TestUsername_PeerID(t *testing.T) { + assert.Equal(t, "@username", Username("username").PeerID()) +} + func TestChatType_String(t *testing.T) { for _, test := range []struct { ChatType ChatType @@ -36,6 +40,28 @@ func TestChatType_String(t *testing.T) { } } +func TestChatAction_String(t *testing.T) { + for _, test := range []struct { + ChatAction ChatAction + Want string + }{ + {ChatActionTyping, "typing"}, + {ChatActionUploadPhoto, "upload_photo"}, + {ChatActionUploadVideo, "upload_video"}, + {ChatActionRecordVideo, "record_video"}, + {ChatActionRecordVoice, "record_voice"}, + {ChatActionUploadVoice, "upload_voice"}, + {ChatActionUploadDocument, "upload_document"}, + {ChatActionChooseSticker, "choose_sticker"}, + {ChatActionFindLocation, "find_location"}, + {ChatActionRecordVideoNote, "record_video_note"}, + {ChatActionUploadVideoNote, "upload_video_note"}, + {ChatAction(-1), "unknown"}, + } { + assert.Equal(t, test.Want, test.ChatAction.String()) + } +} + func TestChatType_MarshalJSON(t *testing.T) { type sample struct { Type ChatType `json:"type"`