Skip to content

Commit 818a2a5

Browse files
committed
first commit
0 parents  commit 818a2a5

File tree

11 files changed

+774
-0
lines changed

11 files changed

+774
-0
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2014, MessageBird
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.PHONY: examples format
2+
3+
GORUN = go run
4+
GOFMT = gofmt -d=true -s=true -w=true
5+
6+
all:
7+
@echo "make examples - Run all the out-of-the-box workable examples"
8+
@echo "make format - Reformat the .go files in this project"
9+
10+
examples:
11+
$(GORUN) examples/balance.go
12+
$(GORUN) examples/hlr_create.go
13+
$(GORUN) examples/message_create.go
14+
$(GORUN) examples/voice_message_create.go
15+
16+
format:
17+
$(GOFMT) messagebird/messagebird.go
18+
$(GOFMT) examples/*.go

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
MessageBird's REST API for Go
2+
=============================
3+
This repository contains the open source Go client for MessageBird's REST API. Documentation can be found at: https://www.messagebird.com/developers/go.
4+
5+
Requirements
6+
------------
7+
- [Sign up](https://www.messagebird.com/en/signup) for a free MessageBird account
8+
- Create a new access key in the developers sections
9+
- An application written in Go to make use of this API
10+
11+
Installation
12+
------------
13+
The easiest way to use the MessageBird API in your Go project is to install it using *go get*:
14+
15+
```
16+
$ go get https://github.com/messagebird/go-rest-api/messagebird
17+
```
18+
19+
If this doesn't work, you probably haven't set your [GOPATH](https://code.google.com/p/go-wiki/wiki/GOPATH) variable.
20+
21+
Examples
22+
--------
23+
We have put some self-explanatory examples in the [examples](https://github.com/messagebird/go-rest-api/tree/master/examples) directory, but here is a quick example on how to get started. Assuming the **go get** installation worked, you can import the messagebird package like this:
24+
25+
```go
26+
import "github.com/messagebird/go-rest-api/messagebird"
27+
```
28+
29+
Then, create an instance of **messagebird.Client**:
30+
31+
```go
32+
mb := &messagebird.Client{AccessKey: "test_gshuPaZoeEG6ovbc8M79w0QyM"}
33+
```
34+
35+
Now you can query the API for information or send data. For example, if we want to request our balance information you'd do something like this:
36+
37+
```go
38+
// Request the balance information, returned as a Balance object.
39+
balance, err := mb.Balance()
40+
if err != nil {
41+
fmt.Println(err)
42+
os.Exit(1)
43+
}
44+
45+
// Check for errors returned as JSON.
46+
if len(balance.Errors) != 0 {
47+
for _, error := range balance.Errors {
48+
fmt.Println(" code :", error.Code)
49+
fmt.Println(" description :", error.Description)
50+
fmt.Println(" parameter :", error.Parameter)
51+
}
52+
os.Exit(1)
53+
}
54+
55+
fmt.Println(" payment :", balance.Payment)
56+
fmt.Println(" type :", balance.Type)
57+
fmt.Println(" amount :", balance.Amount)
58+
```
59+
60+
This will give you something like:
61+
```shell
62+
$ go run example.go
63+
payment : prepaid
64+
type : credits
65+
amount : 9
66+
```
67+
68+
Please see the other examples for a complete overview of all the available API calls.
69+
70+
Documentation
71+
-------------
72+
Complete documentation, instructions, and examples are available at:
73+
[https://www.messagebird.com/developers/go](https://www.messagebird.com/developers/go).
74+
75+
License
76+
-------
77+
The MessageBird REST Client for Ruby is licensed under [The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause). Copyright (c) 2014, MessageBird

examples/balance.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"../messagebird"
5+
"fmt"
6+
"os"
7+
)
8+
9+
var AccessKey = "test_gshuPaZoeEG6ovbc8M79w0QyM"
10+
11+
func main() {
12+
// Create a MessageBird client with the specified AccessKey.
13+
mb := &messagebird.Client{AccessKey: AccessKey}
14+
15+
// Fetch the Balance object.
16+
balance, err := mb.Balance()
17+
if err != nil {
18+
fmt.Println(err)
19+
os.Exit(1)
20+
}
21+
22+
// Check for errors returned as JSON.
23+
if len(balance.Errors) != 0 {
24+
for _, error := range balance.Errors {
25+
fmt.Println(" code :", error.Code)
26+
fmt.Println(" description :", error.Description)
27+
fmt.Println(" parameter :", error.Parameter, "\n")
28+
}
29+
os.Exit(1)
30+
}
31+
32+
// Print the object information.
33+
fmt.Println("\nThe following information was returned as a Balance object:\n")
34+
fmt.Println(" payment :", balance.Payment)
35+
fmt.Println(" type :", balance.Type)
36+
fmt.Println(" amount :", balance.Amount, "\n")
37+
}

examples/hlr.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"../messagebird"
5+
"fmt"
6+
"os"
7+
)
8+
9+
var AccessKey = ""
10+
var HLRId = ""
11+
12+
func main() {
13+
if len(AccessKey) == 0 || len(HLRId) == 0 {
14+
fmt.Println("You need to set an AccessKey and HLRId in this file")
15+
os.Exit(1)
16+
}
17+
18+
// Create a MessageBird client with the specified AccessKey.
19+
mb := &messagebird.Client{AccessKey: AccessKey}
20+
21+
// Fetch the HLR object.
22+
hlr, err := mb.HLR("d26c94c0353bd8e171a3979h97860638")
23+
if err != nil {
24+
fmt.Println(err)
25+
os.Exit(1)
26+
}
27+
28+
// Check for errors returned as JSON.
29+
if len(hlr.Errors) != 0 {
30+
for _, error := range hlr.Errors {
31+
fmt.Println(" code :", error.Code)
32+
fmt.Println(" description :", error.Description)
33+
fmt.Println(" parameter :", error.Parameter, "\n")
34+
}
35+
os.Exit(1)
36+
}
37+
38+
// Print the object information.
39+
fmt.Println("\nThe following information was returned as an HLR object:\n")
40+
fmt.Println(" id :", hlr.Id)
41+
fmt.Println(" href :", hlr.HRef)
42+
fmt.Println(" msisdn :", hlr.MSISDN)
43+
fmt.Println(" reference :", hlr.Reference)
44+
fmt.Println(" status :", hlr.Status)
45+
fmt.Println(" createdDatetime :", hlr.CreatedDatetime)
46+
fmt.Println(" statusDatetime :", hlr.StatusDatetime, "\n")
47+
}

examples/hlr_create.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"../messagebird"
5+
"fmt"
6+
"os"
7+
)
8+
9+
var AccessKey = "test_gshuPaZoeEG6ovbc8M79w0QyM"
10+
11+
func main() {
12+
// Create a MessageBird client with the specified AccessKey.
13+
mb := &messagebird.Client{AccessKey: AccessKey}
14+
15+
// Fetch the HLR object.
16+
hlr, err := mb.CreateHLR("31612345678", "MyReference")
17+
if err != nil {
18+
fmt.Println(err)
19+
os.Exit(1)
20+
}
21+
22+
// Check for errors returned as JSON.
23+
if len(hlr.Errors) != 0 {
24+
for _, error := range hlr.Errors {
25+
fmt.Println(" code :", error.Code)
26+
fmt.Println(" description :", error.Description)
27+
fmt.Println(" parameter :", error.Parameter, "\n")
28+
}
29+
os.Exit(1)
30+
}
31+
32+
// Print the object information.
33+
fmt.Println("\nThe following information was returned as an HLR object:\n")
34+
fmt.Println(" id :", hlr.Id)
35+
fmt.Println(" href :", hlr.HRef)
36+
fmt.Println(" msisdn :", hlr.MSISDN)
37+
fmt.Println(" reference :", hlr.Reference)
38+
fmt.Println(" status :", hlr.Status)
39+
fmt.Println(" createdDatetime :", hlr.CreatedDatetime)
40+
fmt.Println(" statusDatetime :", hlr.StatusDatetime, "\n")
41+
}

examples/message.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"../messagebird"
5+
"fmt"
6+
"os"
7+
)
8+
9+
var AccessKey = ""
10+
var MessageId = ""
11+
12+
func main() {
13+
if len(AccessKey) == 0 || len(MessageId) == 0 {
14+
fmt.Println("You need to set an AccessKey and MessageId in this file")
15+
os.Exit(1)
16+
}
17+
18+
// Create a MessageBird client with the specified AccessKey.
19+
mb := &messagebird.Client{AccessKey: AccessKey}
20+
21+
// Fetch the Message object.
22+
message, err := mb.Message(MessageId)
23+
if err != nil {
24+
fmt.Println(err)
25+
os.Exit(1)
26+
}
27+
28+
// Check for errors returned as JSON.
29+
if len(message.Errors) != 0 {
30+
for _, error := range message.Errors {
31+
fmt.Println(" code :", error.Code)
32+
fmt.Println(" description :", error.Description)
33+
fmt.Println(" parameter :", error.Parameter, "\n")
34+
}
35+
os.Exit(1)
36+
}
37+
38+
// Print the object information.
39+
fmt.Println("\nThe following information was returned as a Message object:\n")
40+
fmt.Println(" id :", message.Id)
41+
fmt.Println(" href :", message.HRef)
42+
fmt.Println(" direction :", message.Direction)
43+
fmt.Println(" type :", message.Type)
44+
fmt.Println(" originator :", message.Originator)
45+
fmt.Println(" body :", message.Body)
46+
fmt.Println(" reference :", message.Reference)
47+
fmt.Println(" validity :", message.Validity)
48+
fmt.Println(" gateway :", message.Gateway)
49+
50+
if len(message.TypeDetails) > 0 {
51+
fmt.Println(" typeDetails")
52+
for k, v := range message.TypeDetails {
53+
fmt.Println(" ", k, " : ", v)
54+
}
55+
}
56+
57+
fmt.Println(" datacoding :", message.DataCoding)
58+
fmt.Println(" mclass :", message.MClass)
59+
fmt.Println(" scheduledDatetime :", message.ScheduledDatetime)
60+
fmt.Println(" createdDatetime :", message.CreatedDatetime)
61+
fmt.Println(" recipients")
62+
fmt.Println(" totalCount :", message.Recipients.TotalCount)
63+
fmt.Println(" totalSentCount :", message.Recipients.TotalSentCount)
64+
fmt.Println(" totalDeliveredCount :", message.Recipients.TotalDeliveredCount)
65+
fmt.Println(" TotalDeliveryFailedCount :", message.Recipients.TotalDeliveryFailedCount)
66+
fmt.Println(" items")
67+
68+
for _, recipient := range message.Recipients.Items {
69+
fmt.Println(" recipient :", recipient.Recipient)
70+
fmt.Println(" status :", recipient.Status)
71+
fmt.Println(" statusDatetime :", recipient.StatusDatetime, "\n")
72+
}
73+
}

examples/message_create.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"../messagebird"
5+
"fmt"
6+
"net/url"
7+
"os"
8+
)
9+
10+
var AccessKey = "test_gshuPaZoeEG6ovbc8M79w0QyM"
11+
12+
func main() {
13+
// Create a MessageBird client with the specified AccessKey.
14+
mb := &messagebird.Client{AccessKey: AccessKey}
15+
16+
// The optional parameters.
17+
params := &url.Values{"reference": {"MyReference"}}
18+
19+
// Fetch the Message object.
20+
message, err := mb.CreateMessage("MyName", []string{"31612345678"}, "Hello World", params)
21+
if err != nil {
22+
fmt.Println(err)
23+
os.Exit(1)
24+
}
25+
26+
// Check for errors returned as JSON.
27+
if len(message.Errors) != 0 {
28+
for _, error := range message.Errors {
29+
fmt.Println(" code :", error.Code)
30+
fmt.Println(" description :", error.Description)
31+
fmt.Println(" parameter :", error.Parameter, "\n")
32+
}
33+
os.Exit(1)
34+
}
35+
36+
// Print the object information.
37+
fmt.Println("\nThe following information was returned as a Message object:\n")
38+
fmt.Println(" id :", message.Id)
39+
fmt.Println(" href :", message.HRef)
40+
fmt.Println(" direction :", message.Direction)
41+
fmt.Println(" type :", message.Type)
42+
fmt.Println(" originator :", message.Originator)
43+
fmt.Println(" body :", message.Body)
44+
fmt.Println(" reference :", message.Reference)
45+
fmt.Println(" validity :", message.Validity)
46+
fmt.Println(" gateway :", message.Gateway)
47+
48+
if len(message.TypeDetails) > 0 {
49+
fmt.Println(" typeDetails")
50+
for k, v := range message.TypeDetails {
51+
fmt.Println(" ", k, " : ", v)
52+
}
53+
}
54+
55+
fmt.Println(" datacoding :", message.DataCoding)
56+
fmt.Println(" mclass :", message.MClass)
57+
fmt.Println(" scheduledDatetime :", message.ScheduledDatetime)
58+
fmt.Println(" createdDatetime :", message.CreatedDatetime)
59+
fmt.Println(" recipients")
60+
fmt.Println(" totalCount :", message.Recipients.TotalCount)
61+
fmt.Println(" totalSentCount :", message.Recipients.TotalSentCount)
62+
fmt.Println(" totalDeliveredCount :", message.Recipients.TotalDeliveredCount)
63+
fmt.Println(" TotalDeliveryFailedCount :", message.Recipients.TotalDeliveryFailedCount)
64+
fmt.Println(" items")
65+
66+
for _, recipient := range message.Recipients.Items {
67+
fmt.Println(" recipient :", recipient.Recipient)
68+
fmt.Println(" status :", recipient.Status)
69+
fmt.Println(" statusDatetime :", recipient.StatusDatetime, "\n")
70+
}
71+
}

0 commit comments

Comments
 (0)