github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/actions.go (about)

     1  // Copyright 2018 LINE Corporation
     2  //
     3  // LINE Corporation licenses this file to you under the Apache License,
     4  // version 2.0 (the "License"); you may not use this file except in compliance
     5  // with the License. You may obtain a copy of the License at:
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package linebot
    16  
    17  import "encoding/json"
    18  
    19  // ActionType type
    20  type ActionType string
    21  
    22  // ActionType constants
    23  const (
    24  	ActionTypeURI            ActionType = "uri"
    25  	ActionTypeMessage        ActionType = "message"
    26  	ActionTypePostback       ActionType = "postback"
    27  	ActionTypeDatetimePicker ActionType = "datetimepicker"
    28  	ActionTypeCamera         ActionType = "camera"
    29  	ActionTypeCameraRoll     ActionType = "cameraRoll"
    30  	ActionTypeLocation       ActionType = "location"
    31  )
    32  
    33  // InputOption type
    34  type InputOption string
    35  
    36  // InputOption constants
    37  const (
    38  	InputOptionCloseRichMenu InputOption = "closeRichMenu"
    39  	InputOptionOpenRichMenu  InputOption = "openRichMenu"
    40  	InputOptionOpenKeyboard  InputOption = "openKeyboard"
    41  	InputOptionOpenVoice     InputOption = "openVoice"
    42  )
    43  
    44  // Action interface
    45  type Action interface {
    46  	json.Marshaler
    47  }
    48  
    49  // TemplateAction interface
    50  type TemplateAction interface {
    51  	Action
    52  	TemplateAction()
    53  }
    54  
    55  // QuickReplyAction type
    56  type QuickReplyAction interface {
    57  	Action
    58  	QuickReplyAction()
    59  }
    60  
    61  // URIAction type
    62  type URIAction struct {
    63  	Label  string
    64  	URI    string
    65  	AltURI *URIActionAltURI
    66  }
    67  
    68  // URIActionAltURI type
    69  type URIActionAltURI struct {
    70  	Desktop string `json:"desktop"`
    71  }
    72  
    73  // MarshalJSON method of URIAction
    74  func (a *URIAction) MarshalJSON() ([]byte, error) {
    75  	return json.Marshal(&struct {
    76  		Type   ActionType       `json:"type"`
    77  		Label  string           `json:"label,omitempty"`
    78  		URI    string           `json:"uri"`
    79  		AltURI *URIActionAltURI `json:"altUri,omitempty"`
    80  	}{
    81  		Type:   ActionTypeURI,
    82  		Label:  a.Label,
    83  		URI:    a.URI,
    84  		AltURI: a.AltURI,
    85  	})
    86  }
    87  
    88  // MessageAction type
    89  type MessageAction struct {
    90  	Label string
    91  	Text  string
    92  }
    93  
    94  // MarshalJSON method of MessageAction
    95  func (a *MessageAction) MarshalJSON() ([]byte, error) {
    96  	return json.Marshal(&struct {
    97  		Type  ActionType `json:"type"`
    98  		Label string     `json:"label,omitempty"`
    99  		Text  string     `json:"text"`
   100  	}{
   101  		Type:  ActionTypeMessage,
   102  		Label: a.Label,
   103  		Text:  a.Text,
   104  	})
   105  }
   106  
   107  // PostbackAction type
   108  type PostbackAction struct {
   109  	Label       string
   110  	Data        string
   111  	Text        string
   112  	DisplayText string
   113  	InputOption InputOption
   114  	FillInText  string
   115  }
   116  
   117  // MarshalJSON method of PostbackAction
   118  func (a *PostbackAction) MarshalJSON() ([]byte, error) {
   119  	return json.Marshal(&struct {
   120  		Type        ActionType `json:"type"`
   121  		Label       string      `json:"label,omitempty"`
   122  		Data        string      `json:"data"`
   123  		Text        string      `json:"text,omitempty"`
   124  		DisplayText string      `json:"displayText,omitempty"`
   125  		InputOption InputOption `json:"inputOption,omitempty"`
   126  		FillInText  string      `json:"fillInText,omitempty"`
   127  	}{
   128  		Type:        ActionTypePostback,
   129  		Label:       a.Label,
   130  		Data:        a.Data,
   131  		Text:        a.Text,
   132  		DisplayText: a.DisplayText,
   133  		InputOption: a.InputOption,
   134  		FillInText:  a.FillInText,
   135  	})
   136  }
   137  
   138  // DatetimePickerAction type
   139  type DatetimePickerAction struct {
   140  	Label   string
   141  	Data    string
   142  	Mode    string
   143  	Initial string
   144  	Max     string
   145  	Min     string
   146  }
   147  
   148  // MarshalJSON method of DatetimePickerAction
   149  func (a *DatetimePickerAction) MarshalJSON() ([]byte, error) {
   150  	return json.Marshal(&struct {
   151  		Type    ActionType `json:"type"`
   152  		Label   string     `json:"label,omitempty"`
   153  		Data    string     `json:"data"`
   154  		Mode    string     `json:"mode"`
   155  		Initial string     `json:"initial,omitempty"`
   156  		Max     string     `json:"max,omitempty"`
   157  		Min     string     `json:"min,omitempty"`
   158  	}{
   159  		Type:    ActionTypeDatetimePicker,
   160  		Label:   a.Label,
   161  		Data:    a.Data,
   162  		Mode:    a.Mode,
   163  		Initial: a.Initial,
   164  		Max:     a.Max,
   165  		Min:     a.Min,
   166  	})
   167  }
   168  
   169  // CameraAction type
   170  type CameraAction struct {
   171  	Label string
   172  }
   173  
   174  // MarshalJSON method of CameraAction
   175  func (a *CameraAction) MarshalJSON() ([]byte, error) {
   176  	return json.Marshal(&struct {
   177  		Type  ActionType `json:"type"`
   178  		Label string     `json:"label"`
   179  	}{
   180  		Type:  ActionTypeCamera,
   181  		Label: a.Label,
   182  	})
   183  }
   184  
   185  // CameraRollAction type
   186  type CameraRollAction struct {
   187  	Label string
   188  }
   189  
   190  // MarshalJSON method of CameraRollAction
   191  func (a *CameraRollAction) MarshalJSON() ([]byte, error) {
   192  	return json.Marshal(&struct {
   193  		Type  ActionType `json:"type"`
   194  		Label string     `json:"label"`
   195  	}{
   196  		Type:  ActionTypeCameraRoll,
   197  		Label: a.Label,
   198  	})
   199  }
   200  
   201  // LocationAction type
   202  type LocationAction struct {
   203  	Label string
   204  }
   205  
   206  // MarshalJSON method of LocationAction
   207  func (a *LocationAction) MarshalJSON() ([]byte, error) {
   208  	return json.Marshal(&struct {
   209  		Type  ActionType `json:"type"`
   210  		Label string     `json:"label"`
   211  	}{
   212  		Type:  ActionTypeLocation,
   213  		Label: a.Label,
   214  	})
   215  }
   216  
   217  // TemplateAction implements TemplateAction interface
   218  func (*URIAction) TemplateAction() {}
   219  
   220  // TemplateAction implements TemplateAction interface
   221  func (*MessageAction) TemplateAction() {}
   222  
   223  // TemplateAction implements TemplateAction interface
   224  func (*PostbackAction) TemplateAction() {}
   225  
   226  // TemplateAction implements TemplateAction interface
   227  func (*DatetimePickerAction) TemplateAction() {}
   228  
   229  // QuickReplyAction implements QuickReplyAction interface
   230  func (*MessageAction) QuickReplyAction() {}
   231  
   232  // QuickReplyAction implements QuickReplyAction interface
   233  func (*PostbackAction) QuickReplyAction() {}
   234  
   235  // QuickReplyAction implements QuickReplyAction interface
   236  func (*DatetimePickerAction) QuickReplyAction() {}
   237  
   238  // QuickReplyAction implements QuickReplyAction interface
   239  func (*CameraAction) QuickReplyAction() {}
   240  
   241  // QuickReplyAction implements QuickReplyAction interface
   242  func (*CameraRollAction) QuickReplyAction() {}
   243  
   244  // QuickReplyAction implements QuickReplyAction interface
   245  func (*LocationAction) QuickReplyAction() {}
   246  
   247  // QuickReplyAction implements URI's QuickReplyAction interface
   248  func (*URIAction) QuickReplyAction() {}
   249  
   250  // NewURIAction function
   251  func NewURIAction(label, uri string) *URIAction {
   252  	return &URIAction{
   253  		Label: label,
   254  		URI:   uri,
   255  	}
   256  }
   257  
   258  // NewMessageAction function
   259  func NewMessageAction(label, text string) *MessageAction {
   260  	return &MessageAction{
   261  		Label: label,
   262  		Text:  text,
   263  	}
   264  }
   265  
   266  // NewPostbackAction function
   267  func NewPostbackAction(label, data, text, displayText string, inputOption InputOption, fillInText string) *PostbackAction {
   268  	return &PostbackAction{
   269  		Label:       label,
   270  		Data:        data,
   271  		Text:        text,
   272  		DisplayText: displayText,
   273  		InputOption: inputOption,
   274  		FillInText:  fillInText,
   275  	}
   276  }
   277  
   278  // NewDatetimePickerAction function
   279  func NewDatetimePickerAction(label, data, mode, initial, max, min string) *DatetimePickerAction {
   280  	return &DatetimePickerAction{
   281  		Label:   label,
   282  		Data:    data,
   283  		Mode:    mode,
   284  		Initial: initial,
   285  		Max:     max,
   286  		Min:     min,
   287  	}
   288  }
   289  
   290  // NewCameraAction function
   291  func NewCameraAction(label string) *CameraAction {
   292  	return &CameraAction{
   293  		Label: label,
   294  	}
   295  }
   296  
   297  // NewCameraRollAction function
   298  func NewCameraRollAction(label string) *CameraRollAction {
   299  	return &CameraRollAction{
   300  		Label: label,
   301  	}
   302  }
   303  
   304  // NewLocationAction function
   305  func NewLocationAction(label string) *LocationAction {
   306  	return &LocationAction{
   307  		Label: label,
   308  	}
   309  }