-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from MikeMwita/devmike
Devmike
- Loading branch information
Showing
7 changed files
with
211 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package sms_sender | ||
|
||
import ( | ||
"fmt" | ||
"github.com/MikeMwita/africastalking-go/pkg/sms" | ||
"log" | ||
) | ||
|
||
func main() { | ||
// Example usage | ||
sender := sms.SmsSender{ | ||
ApiKey: "your_api_key", | ||
ApiUser: "your_api_user", | ||
Recipients: []string{"+1234567890"}, | ||
Message: "Hello, world!", | ||
Sender: "your_sender", | ||
} | ||
|
||
response, err := sender.SendSMS() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Printf("Response: %+v\n", response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/MikeMwita/africastalking-go | ||
|
||
go 1.21.4 | ||
|
||
require github.com/google/uuid v1.6.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= | ||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package sms | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/google/uuid" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
type SmsSender struct { | ||
ApiKey string `json:"api_key"` | ||
ApiUser string `json:"api_user"` | ||
Recipients []string `json:"recipients"` | ||
Message string `json:"message"` | ||
Sender string `json:"sender"` | ||
SmsKey string `json:"sms_key"` | ||
} | ||
|
||
type Recipient struct { | ||
Key string `json:"key"` | ||
Cost string `json:"cost"` | ||
SmsKey string `json:"sms_key"` | ||
MessageId string `json:"message_id"` | ||
MessagePart int `json:"message_part"` | ||
Number string `json:"number"` | ||
Status string `json:"status"` | ||
StatusCode string `json:"status_code"` | ||
} | ||
|
||
type SmsMessageData struct { | ||
Message string `json:"message"` | ||
Cost string `json:"cost"` | ||
Recipients []Recipient `json:"recipients"` | ||
} | ||
|
||
type ErrorResponse struct { | ||
HasError bool `json:"has_error"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type SmsSenderResponse struct { | ||
ErrorResponse ErrorResponse `json:"error_response"` | ||
SmsMessageData SmsMessageData `json:"sms_message_data"` | ||
} | ||
|
||
// SendSMS sends an SMS using the Africa's Talking API | ||
func (s *SmsSender) SendSMS() (SmsSenderResponse, error) { | ||
endpoint := "https://api.africastalking.com/version1/messaging" | ||
parsedURL, err := url.Parse(endpoint) | ||
if err != nil { | ||
return SmsSenderResponse{}, err | ||
} | ||
|
||
body := map[string][]string{ | ||
"username": {s.ApiUser}, | ||
"to": s.Recipients, | ||
"message": {s.Message}, | ||
"from": {s.Sender}, | ||
} | ||
|
||
form := url.Values{} | ||
for key, values := range body { | ||
for _, value := range values { | ||
form.Add(key, value) | ||
} | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodPost, parsedURL.String(), strings.NewReader(form.Encode())) | ||
if err != nil { | ||
return SmsSenderResponse{}, err | ||
} | ||
|
||
req.Header.Set("Accept", "application/json") | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
req.Header.Set("apiKey", s.ApiKey) | ||
|
||
client := &http.Client{} | ||
res, err := client.Do(req) | ||
if err != nil { | ||
return SmsSenderResponse{}, err | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode == http.StatusCreated { | ||
recipients := make([]Recipient, 0) | ||
|
||
var data map[string]interface{} | ||
err = json.NewDecoder(res.Body).Decode(&data) | ||
if err != nil { | ||
return SmsSenderResponse{}, err | ||
} | ||
|
||
smsMessageData := data["SMSMessageData"].(map[string]interface{}) | ||
message := smsMessageData["Message"].(string) | ||
cost := strings.Split(message, " ")[len(message)-1] | ||
recipientsData := smsMessageData["Recipients"].([]interface{}) | ||
|
||
for _, recipient := range recipientsData { | ||
recipientData := recipient.(map[string]interface{}) | ||
|
||
rct := Recipient{ | ||
Key: uuid.New().String(), | ||
Cost: recipientData["cost"].(string), | ||
SmsKey: s.SmsKey, | ||
MessageId: recipientData["messageId"].(string), | ||
MessagePart: int(recipientData["messageParts"].(float64)), | ||
Number: recipientData["number"].(string), | ||
Status: recipientData["status"].(string), | ||
StatusCode: fmt.Sprintf("%v", recipientData["statusCode"]), | ||
} | ||
|
||
recipients = append(recipients, rct) | ||
} | ||
|
||
smsSenderResponse := SmsSenderResponse{ | ||
ErrorResponse: ErrorResponse{ | ||
HasError: false, | ||
}, | ||
SmsMessageData: SmsMessageData{ | ||
Message: message, | ||
Cost: cost, | ||
Recipients: recipients, | ||
}, | ||
} | ||
|
||
return smsSenderResponse, nil | ||
} | ||
|
||
smsSenderResponse := SmsSenderResponse{ | ||
ErrorResponse: ErrorResponse{ | ||
HasError: true, | ||
Message: "Message not sent", | ||
}, | ||
} | ||
|
||
return smsSenderResponse, fmt.Errorf("status code: %d", res.StatusCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package sms | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSendSMS(t *testing.T) { | ||
sender := SmsSender{ | ||
ApiKey: "your_api_key", | ||
ApiUser: "your_api_user", | ||
Recipients: []string{"+1234567890"}, | ||
Message: "Hello, world!", | ||
Sender: "your_sender", | ||
} | ||
|
||
response, err := sender.SendSMS() | ||
if err != nil { | ||
t.Errorf("error occurred: %v", err) | ||
} | ||
|
||
if response.ErrorResponse.HasError { | ||
t.Errorf("error response received: %s", response.ErrorResponse.Message) | ||
} | ||
|
||
if len(response.SmsMessageData.Recipients) == 0 { | ||
t.Errorf("no recipients received in response") | ||
} | ||
|
||
if response.SmsMessageData.Message == "" { | ||
t.Errorf("empty message received in response") | ||
} | ||
} |