-
Notifications
You must be signed in to change notification settings - Fork 0
/
getMsg.go
87 lines (64 loc) · 1.37 KB
/
getMsg.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/gin-gonic/gin"
)
// [
// {
// "msisdn": "+27220000000",
// "message": {
// "id": "i_66EE5680-5F25-42D2-AC45-72CBDF427747",
// "type": "text",
// "text": "hello"
// }
// }
// ]
type MessageBlock struct {
ID string `json:"id"`
Type string `json:"type"`
Text string `json:"text"`
}
type msgRes struct {
Msisdn string `json:"msisdn"`
MessageBlock MessageBlock `json:"message"`
}
func getText(c *gin.Context) {
url := "https://api.ayoba.me/v1/business/message"
access, err := Login()
if err != nil {
fmt.Print(err)
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Print("Error fetching text : ", err)
return
}
bearerToken := access.Token
req.Header.Set("Authorization", "Bearer "+bearerToken)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
}
defer resp.Body.Close()
fmt.Println("Response SStatus:", resp.Status)
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Print(err)
return
}
// fmt.Println(string(body))
var data []msgRes
error := json.Unmarshal(body, &data)
if error != nil {
fmt.Print(error)
return
}
fmt.Println("data: ", data)
c.JSON(http.StatusOK, data)
}