Skip to content

Commit

Permalink
Merge pull request #314 from nanato12/feature/new-postback-action-par…
Browse files Browse the repository at this point in the history
…ameters

Add two parameters to PostbackAction
  • Loading branch information
kkdai authored May 18, 2022
2 parents ea24cc9 + 19a22a8 commit f5a97c8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 48 deletions.
10 changes: 5 additions & 5 deletions examples/kitchensink/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func (app *KitchenSink) handleText(message *linebot.TextMessage, replyToken stri
template := linebot.NewButtonsTemplate(
imageURL, "My button sample", "Hello, my button",
linebot.NewURIAction("Go to line.me", "https://line.me"),
linebot.NewPostbackAction("Say hello1", "hello こんにちは", "", "hello こんにちは"),
linebot.NewPostbackAction("言 hello2", "hello こんにちは", "hello こんにちは", ""),
linebot.NewPostbackAction("Say hello1", "hello こんにちは", "", "hello こんにちは", "", ""),
linebot.NewPostbackAction("言 hello2", "hello こんにちは", "hello こんにちは", "", "", ""),
linebot.NewMessageAction("Say message", "Rice=米"),
)
if _, err := app.bot.ReplyMessage(
Expand All @@ -215,11 +215,11 @@ func (app *KitchenSink) handleText(message *linebot.TextMessage, replyToken stri
linebot.NewCarouselColumn(
imageURL, "hoge", "fuga",
linebot.NewURIAction("Go to line.me", "https://line.me"),
linebot.NewPostbackAction("Say hello1", "hello こんにちは", "", ""),
linebot.NewPostbackAction("Say hello1", "hello こんにちは", "", "", "", ""),
),
linebot.NewCarouselColumn(
imageURL, "hoge", "fuga",
linebot.NewPostbackAction("言 hello2", "hello こんにちは", "hello こんにちは", ""),
linebot.NewPostbackAction("言 hello2", "hello こんにちは", "hello こんにちは", "", "", ""),
linebot.NewMessageAction("Say message", "Rice=米"),
),
)
Expand All @@ -238,7 +238,7 @@ func (app *KitchenSink) handleText(message *linebot.TextMessage, replyToken stri
),
linebot.NewImageCarouselColumn(
imageURL,
linebot.NewPostbackAction("Say hello1", "hello こんにちは", "", ""),
linebot.NewPostbackAction("Say hello1", "hello こんにちは", "", "", "", ""),
),
linebot.NewImageCarouselColumn(
imageURL,
Expand Down
29 changes: 24 additions & 5 deletions linebot/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ const (
ActionTypeLocation ActionType = "location"
)

// InputOption type
type InputOption string

// InputOption constants
const (
InputOptionCloseRichMenu InputOption = "closeRichMenu"
InputOptionOpenRichMenu InputOption = "openRichMenu"
InputOptionOpenKeyboard InputOption = "openKeyboard"
InputOptionOpenVoice InputOption = "openVoice"
)

// Action interface
type Action interface {
json.Marshaler
Expand Down Expand Up @@ -99,22 +110,28 @@ type PostbackAction struct {
Data string
Text string
DisplayText string
InputOption InputOption
FillInText string
}

// MarshalJSON method of PostbackAction
func (a *PostbackAction) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type ActionType `json:"type"`
Label string `json:"label,omitempty"`
Data string `json:"data"`
Text string `json:"text,omitempty"`
DisplayText string `json:"displayText,omitempty"`
Label string `json:"label,omitempty"`
Data string `json:"data"`
Text string `json:"text,omitempty"`
DisplayText string `json:"displayText,omitempty"`
InputOption InputOption `json:"inputOption,omitempty"`
FillInText string `json:"fillInText,omitempty"`
}{
Type: ActionTypePostback,
Label: a.Label,
Data: a.Data,
Text: a.Text,
DisplayText: a.DisplayText,
InputOption: a.InputOption,
FillInText: a.FillInText,
})
}

Expand Down Expand Up @@ -247,12 +264,14 @@ func NewMessageAction(label, text string) *MessageAction {
}

// NewPostbackAction function
func NewPostbackAction(label, data, text, displayText string) *PostbackAction {
func NewPostbackAction(label, data, text, displayText string, inputOption InputOption, fillInText string) *PostbackAction {
return &PostbackAction{
Label: label,
Data: data,
Text: text,
DisplayText: displayText,
InputOption: inputOption,
FillInText: fillInText,
}
}

Expand Down
2 changes: 1 addition & 1 deletion linebot/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestMessageTypes(t *testing.T) {
"https://example.com/bot/images/image.jpg",
"",
"Please select",
NewPostbackAction("Buy", "action=buy&itemid=123", "", "displayText"),
NewPostbackAction("Buy", "action=buy&itemid=123", "", "displayText", "", ""),
),
),
Want: want{
Expand Down
Loading

0 comments on commit f5a97c8

Please sign in to comment.