github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/send_message.go (about) 1 // Copyright 2016 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 ( 18 "bytes" 19 "context" 20 "encoding/json" 21 "io" 22 ) 23 24 // PushMessage method 25 func (client *Client) PushMessage(to string, messages ...SendingMessage) *PushMessageCall { 26 return &PushMessageCall{ 27 c: client, 28 to: to, 29 messages: messages, 30 notificationDisabled: false, 31 } 32 } 33 34 // PushMessageCall type 35 type PushMessageCall struct { 36 c *Client 37 ctx context.Context 38 39 to string 40 messages []SendingMessage 41 notificationDisabled bool 42 } 43 44 // WithContext method 45 func (call *PushMessageCall) WithContext(ctx context.Context) *PushMessageCall { 46 call.ctx = ctx 47 return call 48 } 49 50 // WithNotificationDisabled method will disable push notification 51 func (call *PushMessageCall) WithNotificationDisabled() *PushMessageCall { 52 call.notificationDisabled = true 53 return call 54 } 55 56 // WithRetryKey method will set retry key string (UUID) on PushMessage. 57 func (call *PushMessageCall) WithRetryKey(retryKey string) *PushMessageCall { 58 call.c.setRetryKey(retryKey) 59 return call 60 } 61 62 func (call *PushMessageCall) encodeJSON(w io.Writer) error { 63 enc := json.NewEncoder(w) 64 return enc.Encode(&struct { 65 To string `json:"to"` 66 Messages []SendingMessage `json:"messages"` 67 NotificationDisabled bool `json:"notificationDisabled,omitempty"` 68 }{ 69 To: call.to, 70 Messages: call.messages, 71 NotificationDisabled: call.notificationDisabled, 72 }) 73 } 74 75 // Do method 76 func (call *PushMessageCall) Do() (*BasicResponse, error) { 77 var buf bytes.Buffer 78 if err := call.encodeJSON(&buf); err != nil { 79 return nil, err 80 } 81 res, err := call.c.post(call.ctx, APIEndpointPushMessage, &buf) 82 if err != nil { 83 return nil, err 84 } 85 defer closeResponse(res) 86 return decodeToBasicResponse(res) 87 } 88 89 // ReplyMessage method 90 func (client *Client) ReplyMessage(replyToken string, messages ...SendingMessage) *ReplyMessageCall { 91 return &ReplyMessageCall{ 92 c: client, 93 replyToken: replyToken, 94 messages: messages, 95 notificationDisabled: false, 96 } 97 } 98 99 // ReplyMessageCall type 100 type ReplyMessageCall struct { 101 c *Client 102 ctx context.Context 103 104 replyToken string 105 messages []SendingMessage 106 notificationDisabled bool 107 } 108 109 // WithContext method 110 func (call *ReplyMessageCall) WithContext(ctx context.Context) *ReplyMessageCall { 111 call.ctx = ctx 112 return call 113 } 114 115 // WithNotificationDisabled method will disable push notification 116 func (call *ReplyMessageCall) WithNotificationDisabled() *ReplyMessageCall { 117 call.notificationDisabled = true 118 return call 119 } 120 121 func (call *ReplyMessageCall) encodeJSON(w io.Writer) error { 122 enc := json.NewEncoder(w) 123 return enc.Encode(&struct { 124 ReplyToken string `json:"replyToken"` 125 Messages []SendingMessage `json:"messages"` 126 NotificationDisabled bool `json:"notificationDisabled,omitempty"` 127 }{ 128 ReplyToken: call.replyToken, 129 Messages: call.messages, 130 NotificationDisabled: call.notificationDisabled, 131 }) 132 } 133 134 // Do method 135 func (call *ReplyMessageCall) Do() (*BasicResponse, error) { 136 var buf bytes.Buffer 137 if err := call.encodeJSON(&buf); err != nil { 138 return nil, err 139 } 140 res, err := call.c.post(call.ctx, APIEndpointReplyMessage, &buf) 141 if err != nil { 142 return nil, err 143 } 144 defer closeResponse(res) 145 return decodeToBasicResponse(res) 146 } 147 148 // Multicast method 149 func (client *Client) Multicast(to []string, messages ...SendingMessage) *MulticastCall { 150 return &MulticastCall{ 151 c: client, 152 to: to, 153 messages: messages, 154 notificationDisabled: false, 155 } 156 } 157 158 // MulticastCall type 159 type MulticastCall struct { 160 c *Client 161 ctx context.Context 162 163 to []string 164 messages []SendingMessage 165 notificationDisabled bool 166 } 167 168 // WithContext method 169 func (call *MulticastCall) WithContext(ctx context.Context) *MulticastCall { 170 call.ctx = ctx 171 return call 172 } 173 174 // WithNotificationDisabled method will disable push notification 175 func (call *MulticastCall) WithNotificationDisabled() *MulticastCall { 176 call.notificationDisabled = true 177 return call 178 } 179 180 // WithRetryKey method will set retry key string (UUID) on Multicast. 181 func (call *MulticastCall) WithRetryKey(retryKey string) *MulticastCall { 182 call.c.setRetryKey(retryKey) 183 return call 184 } 185 186 func (call *MulticastCall) encodeJSON(w io.Writer) error { 187 enc := json.NewEncoder(w) 188 return enc.Encode(&struct { 189 To []string `json:"to"` 190 Messages []SendingMessage `json:"messages"` 191 NotificationDisabled bool `json:"notificationDisabled,omitempty"` 192 }{ 193 To: call.to, 194 Messages: call.messages, 195 NotificationDisabled: call.notificationDisabled, 196 }) 197 } 198 199 // Do method 200 func (call *MulticastCall) Do() (*BasicResponse, error) { 201 var buf bytes.Buffer 202 if err := call.encodeJSON(&buf); err != nil { 203 return nil, err 204 } 205 res, err := call.c.post(call.ctx, APIEndpointMulticast, &buf) 206 if err != nil { 207 return nil, err 208 } 209 defer closeResponse(res) 210 return decodeToBasicResponse(res) 211 } 212 213 // BroadcastMessage method 214 func (client *Client) BroadcastMessage(messages ...SendingMessage) *BroadcastMessageCall { 215 return &BroadcastMessageCall{ 216 c: client, 217 messages: messages, 218 } 219 } 220 221 // BroadcastMessageCall type 222 type BroadcastMessageCall struct { 223 c *Client 224 ctx context.Context 225 226 messages []SendingMessage 227 } 228 229 // WithContext method 230 func (call *BroadcastMessageCall) WithContext(ctx context.Context) *BroadcastMessageCall { 231 call.ctx = ctx 232 return call 233 } 234 235 // WithRetryKey method will set retry key string (UUID) on BroadcastMessage. 236 func (call *BroadcastMessageCall) WithRetryKey(retryKey string) *BroadcastMessageCall { 237 call.c.setRetryKey(retryKey) 238 return call 239 } 240 241 func (call *BroadcastMessageCall) encodeJSON(w io.Writer) error { 242 enc := json.NewEncoder(w) 243 return enc.Encode(&struct { 244 Messages []SendingMessage `json:"messages"` 245 }{ 246 Messages: call.messages, 247 }) 248 } 249 250 // Do method 251 func (call *BroadcastMessageCall) Do() (*BasicResponse, error) { 252 var buf bytes.Buffer 253 if err := call.encodeJSON(&buf); err != nil { 254 return nil, err 255 } 256 res, err := call.c.post(call.ctx, APIEndpointBroadcastMessage, &buf) 257 if err != nil { 258 return nil, err 259 } 260 defer closeResponse(res) 261 return decodeToBasicResponse(res) 262 } 263 264 // Narrowcast method 265 func (client *Client) Narrowcast(messages ...SendingMessage) *NarrowcastCall { 266 return &NarrowcastCall{ 267 c: client, 268 messages: messages, 269 } 270 } 271 272 // NarrowcastCall type 273 type NarrowcastCall struct { 274 c *Client 275 ctx context.Context 276 277 messages []SendingMessage 278 recipient Recipient 279 filter *Filter 280 limit *NarrowcastMessageLimit 281 } 282 283 // Filter type 284 type Filter struct { 285 Demographic DemographicFilter `json:"demographic"` 286 } 287 288 // NarrowcastMessageLimit type 289 type NarrowcastMessageLimit struct { 290 Max int `json:"max"` 291 UpToRemainingQuota bool `json:"upToRemainingQuota,omitempty"` 292 } 293 294 // WithContext method 295 func (call *NarrowcastCall) WithContext(ctx context.Context) *NarrowcastCall { 296 call.ctx = ctx 297 return call 298 } 299 300 // WithRecipient method will send to specific recipient objects 301 func (call *NarrowcastCall) WithRecipient(recipient Recipient) *NarrowcastCall { 302 call.recipient = recipient 303 return call 304 } 305 306 // WithDemographic method will send to specific recipients filter by demographic 307 func (call *NarrowcastCall) WithDemographic(demographic DemographicFilter) *NarrowcastCall { 308 call.filter = &Filter{Demographic: demographic} 309 return call 310 } 311 312 // WithLimitMax method will set maximum number of recipients 313 func (call *NarrowcastCall) WithLimitMax(max int) *NarrowcastCall { 314 call.limit = &NarrowcastMessageLimit{Max: max} 315 return call 316 } 317 318 // WithLimitMaxUpToRemainingQuota method will set maximum number of recipients but not over remaining quota. 319 func (call *NarrowcastCall) WithLimitMaxUpToRemainingQuota(max int, upToRemainingQuota bool) *NarrowcastCall { 320 call.limit = &NarrowcastMessageLimit{Max: max, UpToRemainingQuota: upToRemainingQuota} 321 return call 322 } 323 324 // WithRetryKey method will set retry key string (UUID) on narrowcast. 325 func (call *NarrowcastCall) WithRetryKey(retryKey string) *NarrowcastCall { 326 call.c.setRetryKey(retryKey) 327 return call 328 } 329 330 func (call *NarrowcastCall) encodeJSON(w io.Writer) error { 331 enc := json.NewEncoder(w) 332 return enc.Encode(&struct { 333 Messages []SendingMessage `json:"messages"` 334 Recipient Recipient `json:"recipient,omitempty"` 335 Filter *Filter `json:"filter,omitempty"` 336 Limit *NarrowcastMessageLimit `json:"limit,omitempty"` 337 }{ 338 Messages: call.messages, 339 Recipient: call.recipient, 340 Filter: call.filter, 341 Limit: call.limit, 342 }) 343 } 344 345 // Do method 346 func (call *NarrowcastCall) Do() (*BasicResponse, error) { 347 var buf bytes.Buffer 348 if err := call.encodeJSON(&buf); err != nil { 349 return nil, err 350 } 351 res, err := call.c.post(call.ctx, APIEndpointNarrowcast, &buf) 352 if err != nil { 353 return nil, err 354 } 355 defer closeResponse(res) 356 return decodeToBasicResponse(res) 357 }