Skip to content

Commit

Permalink
Let's send some text messages 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsirbe committed Apr 10, 2019
0 parents commit e323701
Show file tree
Hide file tree
Showing 12 changed files with 2,311 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2
jobs:
build:
docker:
- image: circleci/golang:1
working_directory: /go/src/github.com/martinsirbe/go-sms
steps:
- checkout
- run: go get -v -t -d ./...
- run: go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
- run: make lint
- run: make test
- run: make build
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
config.yaml
49 changes: 49 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
run:
concurrency: 4
deadline: 1m
issues-exit-code: 1
tests: true
skip-dirs:
- pkg/sms/mocks
output:
format: colored-line-number
print-issued-lines: true
print-linter-name: true
linters-settings:
errcheck:
check-type-assertions: true
check-blank: true
golint:
min-confidence: 0.8
gofmt:
simplify: true
goimports:
local-prefixes: github.com/org/project
gocyclo:
min-complexity: 10
maligned:
suggest-new: true
dupl:
threshold: 100
goconst:
min-len: 3
min-occurrences: 3
lll:
line-length: 120
tab-width: 1
nakedret:
max-func-lines: 30
linters:
enable-all: false
enable:
- errcheck
- golint
- gofmt
- goimports
- gocyclo
- maligned
- dupl
- goconst
- lll
- nakedret
fast: false
7 changes: 7 additions & 0 deletions LICENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2019 Martins Irbe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: go-gen
go-gen:
@go generate ./...

.PHONY: test
test:
@go test -v --cover ./...

.PHONY: build
build:
@go build -o bin/go-sms cmd/go-sms/main.go

.PHONY: lint
lint:
golangci-lint run -v
133 changes: 133 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Go SMS
A simple app for sending Short Message Service (SMS) text messages using AWS Simple Notification Service.

![json_ast_badge](https://img.shields.io/badge/SNS-green.svg?logo=amazon-aws&style=flat)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmartinsirbe%2Fgo-sms.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmartinsirbe%2Fgo-sms?ref=badge_shield)
![code_coverage_badge](https://img.shields.io/badge/Go%20Coverage-84%25-brightgreen.svg?style=flat)
[![CircleCI](https://circleci.com/gh/martinsirbe/go-sms/tree/master.svg?style=svg)](https://circleci.com/gh/martinsirbe/go-sms/tree/master)

## Build
Run `make build`, and you should see `go-sms` binary in `bin` directory.

## Configuration
### Mandatory
* `aws_access_key` - AWS account access key id string.
* `aws_secret_access_key` - AWS account secret access key string.
* `aws_region` - AWS account secret access key string. (Only certain AWS regions are
supported, check [AWS documentation][1].)

### Optional
* `sender_id` - Sender ID which will be visible on the receiver's device. Can be up to 11 alphanumeric characters which
must contain at least one latter. When not set will default to `NOTICE`. This configuration value will be overridden by
the CLI `sender-id` argument. (Note that only certain countries support sender ID, check [AWS documentation][1].)
* `max_price` - The maximum price in USD that you are willing to pay to send the message. Note that
your message won't be sent if the cost to send the message exceeds the set maximum price. This attribute will have
no effect if the limit set for the `MonthlySpendLimit` attribute is exceeded. Check [AWS documentation][2] for SMS prices,
based on this you can determine the possible `max_price`.
* `sms_type` - Signifies SMS type which is being sent. It can be either `Promotional` (default) or
`Transactional`.
* `Promotional` - Noncritical messages with optimised message delivery to incur the lowest cost, e.g. marketing messages.
* `Transactional` - Critical messages with optimised message delivery to achieve the highest reliability, e.g. authentication messages.

See `config_sample.yaml` for an example configuration file.

### CLI Options
You can provide options as environment variables, or pass options as CLI arguments.
```bash
Usage: go-sms [OPTIONS]

Short Message Service (SMS) text message sender using AWS Simple Notification Service.

Options:
--sender-id The sender ID which will appear on the receiver's device. (Optional, if provided will override sender ID provided via configuration file.) (env $SENDER_ID)
--receiver The receiver mobile phone number. (Mandatory) (env $RECEIVER)
--message The text message you wish to send. (Mandatory) (env $MESSAGE)
--config-path The path to the configurations file. (Mandatory) (env $GO_SMS_CONFIG_PATH)
```
* `message` - Can be 160 GSM, 140 ASCII or 70 UCS-2 characters long with a total size limit of 1600 bytes per SMS publish action.
* `config-path` - Should point to the configurations file. You can use `config_sample.yaml` as a reference.
## Examples
### CLI
```bash
go-sms --sender-id=<sender_id> --receiver=<mobile_phone_number> --message=<your_message> --config-path=<path_to_config_file>
```
### Go
```golang
package main
import (
"io/ioutil"
"gopkg.in/yaml.v2"
log "github.com/sirupsen/logrus"
"github.com/martinsirbe/go-sms/pkg/sms"
)
func main() {
configFile, err := ioutil.ReadFile("path/to/config.yaml")
if err != nil {
log.WithError(err).Fatal("failed to load go-sms config.yaml")
}
var config sms.Config
if err = yaml.Unmarshal(configFile, &config); err != nil {
log.WithError(err).Fatal("failed to unmarshal go-sms config.yaml")
}
sender := sms.New(config)
if _, err := sender.Send("Hello world!", "+44xxx"); err != nil {
log.WithError(err).Fatal("failed to send the text message")
}
}
```
## Tests
To run tests, just run `make test`.
```bash
? github.com/martinsirbe/go-sms/cmd/go-sms [no test files]
=== RUN TestSuccessfullyPublishedSMS
--- PASS: TestSuccessfullyPublishedSMS (0.00s)
=== RUN TestOnFailedSMSPublishReturnError
--- PASS: TestOnFailedSMSPublishReturnError (0.00s)
=== RUN TestSuccessfullySetSenderID
--- PASS: TestSuccessfullySetSenderID (0.00s)
=== RUN TestSuccessfullySetMaxPrice
--- PASS: TestSuccessfullySetMaxPrice (0.00s)
=== RUN TestMaxPriceNotSetIfGivenValueIsBellowOneCent
--- PASS: TestMaxPriceNotSetIfGivenValueIsBellowOneCent (0.00s)
=== RUN TestSenderIDNotSetIfGivenValueIsEmptyString
--- PASS: TestSenderIDNotSetIfGivenValueIsEmptyString (0.00s)
=== RUN TestSuccessfullySetMessageTypeAsPromotional
--- PASS: TestSuccessfullySetMessageTypeAsPromotional (0.00s)
=== RUN TestSuccessfullySetMessageTypeAsTransactional
--- PASS: TestSuccessfullySetMessageTypeAsTransactional (0.00s)
=== RUN TestSuccessfullyCreatedMessageAttributesFromConfig
--- PASS: TestSuccessfullyCreatedMessageAttributesFromConfig (0.00s)
=== RUN TestConfigValuesAreOptionalWhenCreatingMessageAttributesFromConfig
--- PASS: TestConfigValuesAreOptionalWhenCreatingMessageAttributesFromConfig (0.00s)
PASS
coverage: 84.8% of statements
ok github.com/martinsirbe/go-sms/pkg/sms 0.020s coverage: 84.8% of statements
? github.com/martinsirbe/go-sms/pkg/sms/mocks [no test files]
```
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENCE.md) file for details.
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmartinsirbe%2Fgo-sms.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmartinsirbe%2Fgo-sms?ref=badge_large)
## Contributing
1. Go get it! `go get github.com/martinsirbe/go-sms`
2. Create your feature branch. (`git checkout -b my-feature-branch`)
3. Commit your changes. (`git commit -m 'Add ...'`)
4. Push to the branch. (`git push origin my-feature-branch`)
5. Create a new pull request.
[1]: https://docs.aws.amazon.com/sns/latest/dg/sms_supported-countries.html
[2]: https://aws.amazon.com/sns/sms-pricing/
[3]: https://github.com/golangci/golangci-lint
72 changes: 72 additions & 0 deletions cmd/go-sms/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"io/ioutil"
"os"

"gopkg.in/yaml.v2"

cli "github.com/jawher/mow.cli"
log "github.com/sirupsen/logrus"

"github.com/martinsirbe/go-sms/pkg/sms"
)

const (
appName = "go-sms"
appDescription = "Short Message Service (SMS) text message sender using AWS Simple Notification Service."
)

func main() {
app := cli.App(appName, appDescription)

senderID := app.String(cli.StringOpt{
Name: "sender-id",
Desc: "The sender ID which will appear on the receiver's device. (Optional, " +
"if provided will override sender ID provided via configuration file.)",
EnvVar: "SENDER_ID",
})
receiver := app.String(cli.StringOpt{
Name: "receiver",
Desc: "The receiver mobile phone number. (Mandatory)",
EnvVar: "RECEIVER",
})
message := app.String(cli.StringOpt{
Name: "message",
Desc: "The text message you wish to send. (Mandatory)",
EnvVar: "MESSAGE",
})
configPath := app.String(cli.StringOpt{
Name: "config-path",
Desc: "The path to the configurations file. (Mandatory)",
EnvVar: "GO_SMS_CONFIG_PATH",
})

app.Action = func() {
configFile, err := ioutil.ReadFile(*configPath)
if err != nil {
log.WithError(err).Fatal("failed to load go-sms config.yaml")
}

var config sms.Config
if err = yaml.Unmarshal(configFile, &config); err != nil {
log.WithError(err).Fatal("failed to unmarshal go-sms config.yaml")
}

sender := sms.New(config)
if senderID != nil {
sender.WithSenderID(*senderID)
}

id, err := sender.Send(*message, *receiver)
if err != nil {
log.WithError(err).Fatal("failed to send the text message")
}

log.Infof("successfully sent a text message to - %s, message id - %s", *receiver, *id)
}

if err := app.Run(os.Args); err != nil {
log.WithError(err).Panicf("app failed to run")
}
}
6 changes: 6 additions & 0 deletions config_sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
aws_access_key: AKIA34K435KLR13KDT666 # this is a dummy value... :)
aws_secret_access_key: lk13hJSFkl35ADfsdDFtkl34fF9D9h9ktjrfaewr # this is a dummy value too... ;)
aws_region: eu-west-1
sender_id: JohnDoe
max_price: 0.03
sms_type: Promotional
Loading

0 comments on commit e323701

Please sign in to comment.