-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmessage.go
1179 lines (987 loc) · 31 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2023 Pius Alfred <me.pius1102@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the “Software”), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package message
//go:generate mockgen -destination=../mocks/message/mock_message.go -package=message -source=message.go
import (
"context"
"fmt"
"net/http"
"sync"
"time"
"github.com/piusalfred/whatsapp/config"
whttp "github.com/piusalfred/whatsapp/pkg/http"
"github.com/piusalfred/whatsapp/pkg/types"
)
type (
Service interface { //nolint:interfacebloat
SendText(ctx context.Context, request *Request[Text]) (*Response, error)
SendLocation(ctx context.Context, request *Request[Location]) (*Response, error)
SendVideo(ctx context.Context, request *Request[Video]) (*Response, error)
SendReaction(ctx context.Context, request *Request[Reaction]) (*Response, error)
SendTemplate(ctx context.Context, request *Request[Template]) (*Response, error)
SendImage(ctx context.Context, request *Request[Image]) (*Response, error)
SendAudio(ctx context.Context, request *Request[Audio]) (*Response, error)
SendDocument(ctx context.Context, request *Request[Document]) (*Response, error)
SendSticker(ctx context.Context, request *Request[Sticker]) (*Response, error)
SendContacts(ctx context.Context, request *Request[Contacts]) (*Response, error)
RequestLocation(ctx context.Context, request *Request[string]) (*Response, error)
SendInteractiveMessage(ctx context.Context, request *Request[Interactive]) (*Response, error)
}
Request[T any] struct {
Recipient string
ReplyTo string
Message *T
}
BaseClient struct {
sender Sender
config config.Reader
}
BaseRequest struct {
Method string
Endpoints []string
Type whttp.RequestType
Message *Message
DecodeOptions whttp.DecodeOptions
Metadata types.Metadata
}
BaseRequestOption func(request *BaseRequest)
)
func NewBaseRequest(message *Message, options ...BaseRequestOption) *BaseRequest {
b := &BaseRequest{
Method: http.MethodPost,
Endpoints: []string{Endpoint},
Type: whttp.RequestTypeSendMessage,
Message: message,
DecodeOptions: whttp.DecodeOptions{
DisallowUnknownFields: true,
DisallowEmptyResponse: true,
},
}
for _, option := range options {
if option != nil {
option(b)
}
}
return b
}
func WithBaseRequestDecodeOptions(options whttp.DecodeOptions) BaseRequestOption {
return func(request *BaseRequest) {
request.DecodeOptions = options
}
}
func WithBaseRequestMetadata(metadata map[string]any) BaseRequestOption {
return func(request *BaseRequest) {
request.Metadata = metadata
}
}
func WithBaseRequestEndpoints(endpoint ...string) BaseRequestOption {
return func(request *BaseRequest) {
request.Endpoints = endpoint
}
}
func WithBaseRequestMethod(method string) BaseRequestOption {
return func(request *BaseRequest) {
if method != "" {
request.Method = method
}
}
}
func WithBaseRequestType(reqType whttp.RequestType) BaseRequestOption {
return func(request *BaseRequest) {
request.Type = reqType
}
}
func NewBaseClient(sender whttp.Sender[Message], reader config.Reader,
middlewares ...SenderMiddleware,
) (*BaseClient, error) {
s := &BaseSender{sender}
sf := s.Send
if len(middlewares) > 0 {
for i := len(middlewares) - 1; i >= 0; i-- {
mw := middlewares[i]
if mw != nil {
sf = mw(sf)
}
}
}
c := &BaseClient{
sender: SenderFunc(sf),
config: reader,
}
return c, nil
}
func (c *BaseClient) SetConfigReader(fetcher config.Reader) {
c.config = fetcher
}
func (c *BaseClient) SendMessage(ctx context.Context, message *Message) (*Response, error) {
conf, err := c.config.Read(ctx)
if err != nil {
return nil, fmt.Errorf("base client: send message: read config: %w", err)
}
req := NewBaseRequest(
message,
WithBaseRequestMethod(http.MethodPost),
WithBaseRequestEndpoints(Endpoint),
WithBaseRequestType(whttp.RequestTypeSendMessage),
WithBaseRequestDecodeOptions(whttp.DecodeOptions{
DisallowUnknownFields: true,
DisallowEmptyResponse: true,
InspectResponseError: true,
}),
)
response, err := c.sender.Send(ctx, conf, req)
if err != nil {
return nil, fmt.Errorf("base client: send message: %w", err)
}
return response, nil
}
func (c *BaseClient) UpdateStatus(ctx context.Context, request *StatusUpdateRequest) (*StatusUpdateResponse, error) {
ms := string(request.Status)
message := &Message{
Product: MessagingProduct,
Status: &ms,
MessageID: &request.MessageID,
}
req := NewBaseRequest(
message,
WithBaseRequestMethod(http.MethodPut),
WithBaseRequestEndpoints(Endpoint),
WithBaseRequestType(whttp.RequestTypeUpdateStatus),
WithBaseRequestDecodeOptions(whttp.DecodeOptions{
DisallowUnknownFields: true,
DisallowEmptyResponse: false,
}),
)
conf, err := c.config.Read(ctx)
if err != nil {
return nil, fmt.Errorf("base client: update message status: read config: %w", err)
}
response, err := c.sender.Send(ctx, conf, req)
if err != nil {
return nil, fmt.Errorf("base client: update message status: %w", err)
}
return &StatusUpdateResponse{Success: response.Success}, nil
}
type (
Client struct {
mu *sync.Mutex
reader config.Reader
config *config.Config
sender Sender
}
)
func (c *Client) ReloadConfig(ctx context.Context) error {
c.mu.Lock()
defer c.mu.Unlock()
var err error
c.config, err = c.reader.Read(ctx)
if err != nil {
return fmt.Errorf("reload config: %w", err)
}
return nil
}
func NewClient(ctx context.Context, reader config.Reader, sender whttp.Sender[Message],
middlewares ...SenderMiddleware,
) (*Client, error) {
conf, err := reader.Read(ctx)
if err != nil {
return nil, fmt.Errorf("read config: %w", err)
}
s := &BaseSender{sender}
sf := s.Send
if len(middlewares) > 0 {
for i := len(middlewares) - 1; i >= 0; i-- {
mw := middlewares[i]
if mw != nil {
sf = mw(sf)
}
}
}
c := &Client{
mu: &sync.Mutex{},
reader: reader,
config: conf,
sender: SenderFunc(sf),
}
return c, nil
}
func (c *Client) SendMessage(ctx context.Context, message *Message) (*Response, error) {
req := NewBaseRequest(
message,
WithBaseRequestMethod(http.MethodPost),
WithBaseRequestEndpoints(Endpoint),
WithBaseRequestType(whttp.RequestTypeSendMessage),
WithBaseRequestDecodeOptions(whttp.DecodeOptions{
DisallowUnknownFields: true,
DisallowEmptyResponse: true,
InspectResponseError: true,
}),
)
response, err := c.sender.Send(ctx, c.config, req)
if err != nil {
return nil, fmt.Errorf("send message: %w", err)
}
return response, nil
}
func (c *Client) UpdateStatus(ctx context.Context, request *StatusUpdateRequest) (*StatusUpdateResponse, error) {
ms := string(request.Status)
message := &Message{
Product: MessagingProduct,
Status: &ms,
MessageID: &request.MessageID,
}
req := NewBaseRequest(
message,
WithBaseRequestMethod(http.MethodPut),
WithBaseRequestEndpoints(Endpoint),
WithBaseRequestType(whttp.RequestTypeUpdateStatus),
WithBaseRequestDecodeOptions(whttp.DecodeOptions{
DisallowUnknownFields: true,
DisallowEmptyResponse: false,
InspectResponseError: true,
}),
)
response, err := c.sender.Send(ctx, c.config, req)
if err != nil {
return nil, fmt.Errorf("update message status: %w", err)
}
return &StatusUpdateResponse{Success: response.Success}, nil
}
const (
StatusSent status = "sent"
StatusDelivered status = "delivered"
StatusRead status = "read"
StatusFailed status = "failed"
StatusDeleted status = "deleted"
StatusWarning status = "warning"
)
type (
status string
StatusUpdateResponse struct {
Success bool `json:"success"`
}
StatusUpdateRequest struct {
MessageID string
Status status
}
StatusUpdater interface {
UpdateStatus(ctx context.Context,
request *StatusUpdateRequest) (*StatusUpdateResponse, error)
}
UpdateStatusFunc func(ctx context.Context,
request *StatusUpdateRequest) (*StatusUpdateResponse, error)
)
func (fn UpdateStatusFunc) UpdateStatus(ctx context.Context,
request *StatusUpdateRequest,
) (*StatusUpdateResponse, error) {
return fn(ctx, request)
}
var (
_ StatusUpdater = (*BaseClient)(nil)
_ StatusUpdater = (*Client)(nil)
)
func RetrieveMessageMetadata(ctx context.Context) types.Metadata {
metadata, ok := ctx.Value(whttp.MessageContextKey(whttp.MessageMetadataContextKey)).(types.Metadata)
if !ok {
return nil
}
return metadata
}
type (
BaseSender struct {
Sender whttp.Sender[Message]
}
SenderFunc func(ctx context.Context, conf *config.Config, request *BaseRequest) (*Response, error)
Sender interface {
Send(ctx context.Context, conf *config.Config, request *BaseRequest) (*Response, error)
}
SenderMiddleware func(senderFunc SenderFunc) SenderFunc
)
func (fn SenderFunc) Send(ctx context.Context, conf *config.Config, request *BaseRequest) (*Response, error) {
return fn(ctx, conf, request)
}
func (c *BaseSender) Send(ctx context.Context, conf *config.Config, request *BaseRequest) (*Response, error) {
options := []whttp.RequestOption[Message]{
whttp.WithRequestEndpoints[Message](conf.APIVersion, conf.PhoneNumberID, Endpoint),
whttp.WithRequestBearer[Message](conf.AccessToken),
whttp.WithRequestType[Message](request.Type),
whttp.WithRequestAppSecret[Message](conf.AppSecret),
whttp.WithRequestSecured[Message](conf.SecureRequests),
whttp.WithRequestMessage[Message](request.Message),
whttp.WithRequestMetadata[Message](request.Metadata),
}
req := whttp.MakeRequest[Message](request.Method, conf.BaseURL, options...)
response := &Response{}
decoder := whttp.ResponseDecoderJSON(response, request.DecodeOptions)
if err := c.Sender.Send(ctx, req, decoder); err != nil {
return nil, fmt.Errorf("base client: send request: %w", err)
}
return response, nil
}
func NewRequest[T any](recipient string, message *T, replyTo string) *Request[T] {
return &Request[T]{Recipient: recipient, Message: message, ReplyTo: replyTo}
}
func buildOptions[T any](message *T, replyTo string, createMessageFunc func(*T) Option) []Option {
options := make([]Option, 1, 2)
options[0] = createMessageFunc(message)
if replyTo != "" {
options = append(options, WithMessageAsReplyTo(replyTo))
}
return options
}
func (c *BaseClient) SendText(ctx context.Context, request *Request[Text]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithTextMessage)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) SendLocation(ctx context.Context, request *Request[Location]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithLocationMessage)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) SendVideo(ctx context.Context, request *Request[Video]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithVideo)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) SendReaction(ctx context.Context, request *Request[Reaction]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithReaction)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) SendTemplate(ctx context.Context, request *Request[Template]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithTemplateMessage)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) SendImage(ctx context.Context, request *Request[Image]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithImage)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) SendAudio(ctx context.Context, request *Request[Audio]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithAudio)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) RequestLocation(ctx context.Context, request *Request[string]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithRequestLocationMessage)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) SendDocument(ctx context.Context, request *Request[Document]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithDocument)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) SendSticker(ctx context.Context, request *Request[Sticker]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithSticker)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) SendContacts(ctx context.Context, request *Request[Contacts]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithContacts)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
func (c *BaseClient) SendInteractiveMessage(ctx context.Context, request *Request[Interactive]) (*Response, error) {
options := buildOptions(request.Message, request.ReplyTo, WithInteractiveMessage)
message, err := New(request.Recipient, options...)
if err != nil {
return nil, err
}
return c.SendMessage(ctx, message)
}
const (
Endpoint = "/messages"
MessagingProduct = "whatsapp"
RecipientTypeIndividual = "individual"
TypeText = "text"
TypeVideo = "video"
TypeAudio = "audio"
TypeSticker = "sticker"
TypeDocument = "document"
TypeImage = "image"
TypeLocation = "location"
TypeReaction = "reaction"
TypeContacts = "contacts"
TypeInteractive = "interactive"
TypeTemplate = "template"
)
type (
Text struct {
PreviewURL bool `json:"preview_url,omitempty"`
Body string `json:"body,omitempty"`
}
Location struct {
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
Name string `json:"name"`
Address string `json:"address"`
}
Context struct {
MessageID string `json:"message_id"`
}
Reaction struct {
MessageID string `json:"message_id"`
Emoji string `json:"emoji"`
}
Message struct {
Product string `json:"messaging_product"`
To string `json:"to"`
RecipientType string `json:"recipient_type"`
Type string `json:"type"`
PreviewURL bool `json:"preview_url,omitempty"`
Context *Context `json:"config,omitempty"`
Text *Text `json:"text,omitempty"`
Location *Location `json:"location,omitempty"`
Reaction *Reaction `json:"reaction,omitempty"`
Contacts Contacts `json:"contacts,omitempty"`
Interactive *Interactive `json:"interactive,omitempty"`
Document *Document `json:"document,omitempty"`
Sticker *Sticker `json:"sticker,omitempty"`
Video *Video `json:"video,omitempty"`
Image *Image `json:"image,omitempty"`
Audio *Audio `json:"audio,omitempty"`
Status *string `json:"status,omitempty"` // used to update message status
MessageID *string `json:"message_id,omitempty"` // used to update message status
Template *Template `json:"template,omitempty"`
}
Option func(message *Message)
Response struct {
Product string `json:"messaging_product,omitempty"`
Contacts []*ResponseContact `json:"contacts,omitempty"`
Messages []*ID `json:"messages,omitempty"`
MessageMetadata types.Metadata `json:"-"`
Success bool `json:"success"`
}
ID struct {
ID string `json:"id,omitempty"`
MessageStatus string `json:"message_status,omitempty"`
}
ResponseContact struct {
Input string `json:"input"`
WhatsappID string `json:"wa_id"`
}
)
func New(recipient string, options ...Option) (*Message, error) {
msg := &Message{
Product: MessagingProduct,
To: recipient,
RecipientType: RecipientTypeIndividual,
Type: "",
PreviewURL: false,
Context: nil,
Text: nil,
}
for _, option := range options {
if option != nil {
option(msg)
}
}
return msg, nil
}
func WithImage(image *Image) Option {
return func(message *Message) {
message.Type = TypeImage
message.Image = image
}
}
func WithAudio(image *Audio) Option {
return func(message *Message) {
message.Type = TypeAudio
message.Audio = image
}
}
func WithSticker(image *Sticker) Option {
return func(message *Message) {
message.Type = TypeSticker
message.Sticker = image
}
}
func WithVideo(image *Video) Option {
return func(message *Message) {
message.Type = TypeVideo
message.Video = image
}
}
func WithDocument(doc *Document) Option {
return func(message *Message) {
message.Document = doc
message.Type = TypeDocument
}
}
func WithContacts(contacts *Contacts) Option {
return func(message *Message) {
message.Type = TypeContacts
message.Contacts = *contacts
}
}
func WithReaction(reaction *Reaction) Option {
return func(message *Message) {
message.Type = TypeReaction
message.Reaction = reaction
}
}
func WithMessageAsReplyTo(messageID string) Option {
return func(message *Message) {
message.Context = &Context{MessageID: messageID}
}
}
func WithTextMessage(text *Text) Option {
return func(message *Message) {
message.Type = TypeText
message.Text = text
}
}
func WithLocationMessage(location *Location) Option {
return func(message *Message) {
message.Type = TypeLocation
message.Location = location
}
}
type (
MediaInfo struct {
ID string `json:"id,omitempty"`
Caption string `json:"caption,omitempty"`
MimeType string `json:"mime_type,omitempty"`
Sha256 string `json:"sha256,omitempty"`
Filename string `json:"filename,omitempty"`
Animated bool `json:"animated,omitempty"` // used with stickers true if animated
}
Media struct {
ID string `json:"id,omitempty"`
Link string `json:"link,omitempty"`
Caption string `json:"caption,omitempty"`
Filename string `json:"filename,omitempty"`
Provider string `json:"provider,omitempty"`
}
Document struct {
ID string `json:"id,omitempty"`
Link string `json:"link,omitempty"`
Caption string `json:"caption,omitempty"`
Filename string `json:"filename,omitempty"`
}
Video struct {
ID string `json:"id,omitempty"`
Link string `json:"link,omitempty"`
Caption string `json:"caption,omitempty"`
}
Image struct {
ID string `json:"id,omitempty"`
Link string `json:"link,omitempty"`
Caption string `json:"caption,omitempty"`
Filename string `json:"filename,omitempty"`
}
Sticker struct {
ID string `json:"id,omitempty"`
}
Audio struct {
ID string `json:"id,omitempty"`
}
)
type (
Address struct {
Street string `json:"street"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Country string `json:"country"`
CountryCode string `json:"country_code"`
Type string `json:"type"`
}
Addresses []*Address
Email struct {
Email string `json:"email"`
Type string `json:"type"`
}
Emails []*Email
Name struct {
FormattedName string `json:"formatted_name"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
MiddleName string `json:"middle_name"`
Suffix string `json:"suffix"`
Prefix string `json:"prefix"`
}
Org struct {
Company string `json:"company"`
Department string `json:"department"`
Title string `json:"title"`
}
Phone struct {
Phone string `json:"phone"`
Type string `json:"type"`
WaID string `json:"wa_id,omitempty"`
}
Phones []*Phone
URL struct {
URL string `json:"url"`
Type string `json:"type"`
}
Urls []*URL
Contact struct {
Addresses Addresses `json:"addresses,omitempty"`
Birthday string `json:"birthday"`
Emails Emails `json:"emails,omitempty"`
Name *Name `json:"name"`
Org *Org `json:"org"`
Phones Phones `json:"phones,omitempty"`
Urls Urls `json:"urls,omitempty"`
}
Contacts []*Contact
ContactOption func(*Contact)
)
func NewContact(options ...ContactOption) *Contact {
contact := &Contact{}
for _, option := range options {
option(contact)
}
return contact
}
func WithContactName(name *Name) ContactOption {
return func(c *Contact) {
c.Name = name
}
}
func WithContactAddresses(addresses ...*Address) ContactOption {
return func(c *Contact) {
c.Addresses = addresses
}
}
func WithContactOrganization(organization *Org) ContactOption {
return func(c *Contact) {
c.Org = organization
}
}
func WithContactURLs(urls ...*URL) ContactOption {
return func(c *Contact) {
c.Urls = urls
}
}
func WithContactPhones(phones ...*Phone) ContactOption {
return func(c *Contact) {
c.Phones = phones
}
}
func WithContactBirthdays(birthday time.Time) ContactOption {
return func(c *Contact) {
// should be formatted as YYYY-MM-DD
bd := birthday.Format(time.DateOnly)
c.Birthday = bd
}
}
func WithContactEmails(emails ...*Email) ContactOption {
return func(c *Contact) {
c.Emails = emails
}
}
const (
TypeInteractiveLocationRequest = "location_request_message"
TypeInteractiveCTAURL = "cta_url"
TypeInteractiveButton = "button"
TypeInteractiveFlow = "flow"
InteractionActionSendLocation = "send_location"
InteractiveActionCTAURL = "cta_url"
InteractiveActionButtonReply = "reply"
InteractiveActionFlow = "flow"
)
type (
InteractiveMessage string
InteractiveButton struct {
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
ID string `json:"id,omitempty"`
Reply *InteractiveReplyButton `json:"reply,omitempty"`
}
InteractiveReplyButton struct {
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
}
// InteractiveSectionRow contains information about a row in an interactive section.
InteractiveSectionRow struct {
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
}
Product struct {
RetailerID string `json:"product_retailer_id,omitempty"`
}
InteractiveSection struct {
Title string `json:"title,omitempty"`
ProductItems []*Product `json:"product_items,omitempty"`
Rows []*InteractiveSectionRow `json:"rows,omitempty"`
}
InteractiveAction struct {
Button string `json:"button,omitempty"`
Buttons []*InteractiveButton `json:"buttons,omitempty"`
CatalogID string `json:"catalog_id,omitempty"`
ProductRetailerID string `json:"product_retailer_id,omitempty"`
Sections []*InteractiveSection `json:"sections,omitempty"`
Name string `json:"name,omitempty"`
Parameters *InteractiveActionParameters `json:"parameters,omitempty"`
}
InteractiveActionParameters struct {
DisplayText string `json:"display_text,omitempty"`
URL string `json:"url,omitempty"`
FlowMessageVersion string `json:"flow_message_version"`
FlowToken string `json:"flow_token"`
FlowID string `json:"flow_id"`
FlowCTA string `json:"flow_cta"`
FlowAction string `json:"flow_action"`
FlowActionPayload *FlowActionPayload `json:"flow_action_payload"`
}
FlowActionPayload struct {
Screen string `json:"screen"`
Data map[string]interface{} `json:"data"`
}
InteractiveHeader struct {
Document *Document `json:"document,omitempty"`
Image *Image `json:"image,omitempty"`
Video *Video `json:"video,omitempty"`
Text string `json:"text,omitempty"`
Type string `json:"type,omitempty"`
}
// InteractiveFooter contains information about an interactive footer.
InteractiveFooter struct {
Text string `json:"text,omitempty"`
}
// InteractiveBody contains information about an interactive body.
InteractiveBody struct {
Text string `json:"text,omitempty"`
}
Interactive struct {
Type string `json:"type,omitempty"`
Action *InteractiveAction `json:"action,omitempty"`
Body *InteractiveBody `json:"body,omitempty"`
Footer *InteractiveFooter `json:"footer,omitempty"`
Header *InteractiveHeader `json:"header,omitempty"`
}
InteractiveOption func(*Interactive)
)
type InteractiveFlowRequest struct {
Body string `json:"body"`
Header *InteractiveHeader `json:"header"`
Footer string `json:"footer"`
FlowMessageVersion string `json:"flow_message_version"`
FlowToken string `json:"flow_token"`
FlowID string `json:"flow_id"`
FlowCTA string `json:"flow_cta"`
FlowAction string `json:"flow_action"`
FlowScreen string `json:"flow_screen"`
FlowData map[string]any `json:"flow_data"`
}
func WithInteractiveFlow(req *InteractiveFlowRequest) Option {
return func(message *Message) {
content := NewInteractiveMessageContent(
TypeInteractiveFlow,
WithInteractiveFooter(req.Footer),