Skip to content

Commit

Permalink
feat(types): ChatAction enum (#36)
Browse files Browse the repository at this point in the history
* feat(types): define ChatAction enum

* feat(types): ParseMode is implements fmt.Stringer

* feat(request): add stringer method

* feat(types): use ChatAction type and fix type issue

* fix(examples): new ChatAction type and ParseMode use String

* tests(types): add tests for ChatAction and Username
  • Loading branch information
mr-linch authored Jun 28, 2022
1 parent d6333ae commit c148211
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/echo-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/quote-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 15 additions & 15 deletions methods_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions parse_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type ParseMode interface {
Name() string
String() string

// Change separator for next calls
Sep(v string) ParseMode
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions parse_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<b>Hello World</b>", HTML.Bold("Hello", "World"))
Expand All @@ -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"))
Expand All @@ -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"))

Expand Down
8 changes: 4 additions & 4 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand Down
2 changes: 1 addition & 1 deletion tgb/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion tgb/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions types_gen_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions types_gen_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
Expand Down

0 comments on commit c148211

Please sign in to comment.