-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusiness_messaging.go
94 lines (77 loc) · 2.32 KB
/
business_messaging.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
//********************************************************************************************************************//
//
// Copyright (C) 2018 - 2021 J&J Ideenschmiede GmbH <[email protected]>
//
// This file is part of gocm.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor (aka gowizzard)
//
//********************************************************************************************************************//
package gocm
import (
"encoding/json"
)
// MessageBody is to structure the body data
type MessageBody struct {
Messages MessageBodyMessages `json:"messages"`
}
type MessageBodyMessages struct {
Authentication MessageBodyAuthentication `json:"authentication"`
Msg []MessageBodyMsg `json:"msg"`
}
type MessageBodyAuthentication struct {
Producttoken string `json:"producttoken"`
}
type MessageBodyMsg struct {
AllowedChannels []string `json:"allowedChannels"`
From string `json:"from"`
To []MessageBodyTo `json:"to"`
Body MessageBodyBody `json:"body"`
}
type MessageBodyTo struct {
Number string `json:"number"`
}
type MessageBodyBody struct {
Type string `json:"type"`
Content string `json:"content"`
}
// MessageReturn is to decode the json data
type MessageReturn struct {
Details string `json:"details"`
ErrorCode int `json:"errorCode"`
Messages []struct {
To string `json:"to"`
Status string `json:"status"`
Reference interface{} `json:"reference"`
Parts int `json:"parts"`
MessageDetails interface{} `json:"messageDetails"`
MessageErrorCode int `json:"messageErrorCode"`
} `json:"messages"`
}
// Message is to send a new message via the business messaging api
func Message(body MessageBody) (MessageReturn, error) {
// Convert body data
convert, err := json.Marshal(body)
if err != nil {
return MessageReturn{}, err
}
// Config new request
c := Config{
Path: "/message",
Method: "POST",
Body: convert,
}
// Send new request
response, err := c.Send()
if err != nil {
return MessageReturn{}, err
}
// Close request body
defer response.Body.Close()
// Decode data
var decode MessageReturn
json.NewDecoder(response.Body).Decode(&decode)
// Return data
return decode, nil
}