Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding pr template file #9

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bea301d
add token checking expiration checking
cikupin Nov 2, 2019
0f40201
add RenewToken()
cikupin Nov 2, 2019
fa7d02d
update readme
cikupin Nov 5, 2019
eefd209
Merge branch 'feature/renew-jwt-if-expired' into redis-storage
cikupin Nov 6, 2019
7460f98
add redis storage
cikupin Nov 8, 2019
803fa1e
improve redis storage & add getJWTExpiration()
cikupin Nov 8, 2019
860bebd
add token expiration checking
cikupin Nov 11, 2019
cffba98
update go module
cikupin Nov 11, 2019
903d637
Merge pull request #1 from cikupin/redis-storage
cikupin Nov 14, 2019
29cef05
remove verify secret
cikupin Nov 29, 2019
f29f63e
Merge pull request #2 from cikupin/remove-verify-secret
cikupin Nov 29, 2019
25afcaa
fix jwt expired
cikupin Nov 30, 2019
6d77535
Merge pull request #3 from cikupin/fix-jwt-expired
cikupin Nov 30, 2019
c22546c
add send HSM method
cikupin Dec 16, 2019
eac239f
add pre create user & link user to channel method
cikupin Dec 16, 2019
80aeaaf
add basic auth support
cikupin Dec 17, 2019
7021226
add basic auth support
cikupin Dec 17, 2019
5368fd4
add response data for http status & response flag
cikupin Dec 17, 2019
1854333
update unit test
cikupin Dec 17, 2019
2fa10fc
Merge pull request #4 from cikupin/feature/add-user
cikupin Dec 17, 2019
9344dc9
export struct
cikupin Dec 17, 2019
5f77f64
Merge pull request #5 from cikupin/feature/add-user
cikupin Dec 17, 2019
4ad827f
add mapstructure
cikupin Dec 17, 2019
f3e235b
Merge pull request #6 from cikupin/feature/add-user
cikupin Dec 17, 2019
8b7ce6c
allow blank surname
cikupin Jan 6, 2020
29daa7a
Merge pull request #7 from cikupin/allow-blank-surname
cikupin Jan 6, 2020
927a61b
Updating Pull Request Template 2020-02-17
ardityawahyu Feb 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add send HSM method
cikupin committed Dec 16, 2019
commit c22546c371d7847e8097697f1247c86d9d882fc8
30 changes: 30 additions & 0 deletions smooch.go
Original file line number Diff line number Diff line change
@@ -63,7 +63,10 @@ type Client interface {
RenewToken() (string, error)
AddWebhookEventHandler(handler WebhookEventHandler)
Send(userID string, message *Message) (*ResponsePayload, error)
SendHSM(userID string, hsmMessage *HsmMessage) (*ResponsePayload, error)
GetAppUser(userID string) (*AppUser, error)
PreCreateAppUser(userID, surname, givenName string) (*AppUser, error)
LinkAppUserToChannel(channelType, confirmationType, phoneNumber string) (*AppUser, error)
UploadFileAttachment(filepath string, upload AttachmentUpload) (*Attachment, error)
UploadAttachment(r io.Reader, upload AttachmentUpload) (*Attachment, error)
}
@@ -222,6 +225,33 @@ func (sc *smoochClient) Send(userID string, message *Message) (*ResponsePayload,
return &responsePayload, nil
}

// SendHSM will send message using Whatsapp HSM template
func (sc *smoochClient) SendHSM(userID string, hsmMessage *HsmMessage) (*ResponsePayload, error) {
url := sc.getURL(
fmt.Sprintf("/v1.1/apps/%s/appusers/%s/messages", sc.appID, userID),
nil,
)

buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(hsmMessage)
if err != nil {
return nil, err
}

req, err := sc.createRequest(http.MethodPost, url, buf, nil)
if err != nil {
return nil, err
}

var responsePayload ResponsePayload
err = sc.sendRequest(req, &responsePayload)
if err != nil {
return nil, err
}

return &responsePayload, nil
}

func (sc *smoochClient) GetAppUser(userID string) (*AppUser, error) {
url := sc.getURL(
fmt.Sprintf("/v1.1/apps/%s/appusers/%s", sc.appID, userID),
65 changes: 65 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
@@ -107,6 +107,7 @@ type AppUser struct {
Email string `json:"email,omitempty"`
GivenName string `json:"givenName,omitempty"`
Surname string `json:"surname,omitempty"`
HasPaymentInfo bool `json:"hasPaymentInfo,omitmepty"`
}

type AppUserClient struct {
@@ -199,6 +200,70 @@ func (m *Message) MarshalJSON() ([]byte, error) {
return json.Marshal(aux)
}

// HsmLanguage defines hsm language payload
type HsmLanguage struct {
Policy string `json:"policy"`
Code string `json:"code"`
}

// HsmLocalizableParams defines hsm localizable params data
type HsmLocalizableParams struct {
Default interface{} `json:"default"`
}

// HsmPayload defines payload for hsm
type HsmPayload struct {
Namespace string `json:"namespace"`
ElementName string `json:"element_name"`
Language HsmLanguage `json:"language"`
LocalizableParams []HsmLocalizableParams `json:"localizable_params"`
}

// HsmMessageBody defines property for HSM message
type HsmMessageBody struct {
Type MessageType `json:"type"`
Hsm HsmPayload `json:"hsm"`
}

// HsmMessage defines struct payload for Whatsapp HSM message
type HsmMessage struct {
Role Role `json:"role"`
MessageSchema string `json:"messageSchema"`
Message HsmMessageBody `json:"message"`
Received time.Time `json:"received,omitempty"`
}

// UnmarshalJSON will unmarshall whatsapp HSM message
func (hm *HsmMessage) UnmarshalJSON(data []byte) error {
type Alias HsmMessage
aux := &struct {
Received float64 `json:"received"`
*Alias
}{
Alias: (*Alias)(hm),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
seconds := int64(aux.Received)
ns := (int64(aux.Received*1000) - seconds*1000) * nsMultiplier
hm.Received = time.Unix(seconds, ns)
return nil
}

// MarshalJSON will marshall whatsapp HSM message
func (hm *HsmMessage) MarshalJSON() ([]byte, error) {
type Alias HsmMessage
aux := &struct {
Received float64 `json:"received"`
*Alias
}{
Alias: (*Alias)(hm),
}
aux.Received = float64(hm.Received.UnixNano()) / nsMultiplier
return json.Marshal(aux)
}

type MenuPayload struct {
Menu Menu `json:"menu"`
}