Skip to content

Commit

Permalink
Initial support for TextMagic.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdbate committed Feb 3, 2022
1 parent ca9ca58 commit 063e19f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/sachet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/messagebird/sachet/provider/smsc"
"github.com/messagebird/sachet/provider/telegram"
"github.com/messagebird/sachet/provider/tencentcloud"
"github.com/messagebird/sachet/provider/textmagic"
"github.com/messagebird/sachet/provider/turbosms"
"github.com/messagebird/sachet/provider/twilio"

Expand Down Expand Up @@ -72,6 +73,7 @@ var config struct {
Esendex esendex.Config
Sms77 sms77.Config
Ghasedak ghasedak.Config
TextMagic textmagic.Config
}

Receivers []ReceiverConf
Expand Down
3 changes: 3 additions & 0 deletions cmd/sachet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/messagebird/sachet/provider/sms77"
"github.com/messagebird/sachet/provider/smsc"
"github.com/messagebird/sachet/provider/telegram"
"github.com/messagebird/sachet/provider/textmagic"
"github.com/messagebird/sachet/provider/turbosms"
"github.com/messagebird/sachet/provider/twilio"

Expand Down Expand Up @@ -226,6 +227,8 @@ func providerByName(name string) (sachet.Provider, error) {
return sms77.NewSms77(config.Providers.Sms77), nil
case "ghasedak":
return ghasedak.NewGhasedak(config.Providers.Ghasedak), nil
case "textmagic":
return textmagic.NewTextMagic(config.Providers.TextMagic), nil
}

return nil, fmt.Errorf("%s: Unknown provider", name)
Expand Down
3 changes: 3 additions & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ providers:
debug: false
ghasedak:
api_token: 'GHASEDAK_API_KEY'
textmagic:
username: 'username'
api_key: 'TEXTMAGIC_API_KEY'

templates:
- telegram.tmpl
Expand Down
37 changes: 37 additions & 0 deletions provider/textmagic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# TextMagic
## Provider
To configure the TextMagic provider, you need to specify an access key.

```yaml
providers:
textmagic:
username: 'donaldduck'
api_key: 'JNV7NgCKNzQVXNOEpZxwU4c9blGEfF'
```
You can find your username and API Key by going to https://my.textmagic.com/online/api/rest-api/keys.
The TextMagic provider supports SMS text messages only.
A TextMagic trial account can be created at https://www.textmagic.com/.
You can test the TextMagic API without spending SMS credits by using a mobile number that
beings with 999. For example, 999742033616, 999742033617, and so on.
## Receivers
To configure a TextMagic receiver you must specify a list of targets. The `from` field is optional.

```yaml
receivers:
- name: 'team1'
provider: textmagic
to:
- '+999742033616'
- '+999742033617'
- name: 'team2'
provider: textmagic
to:
- '+999742033616'
- '+999742033617'
from: '08039591643'
```
51 changes: 51 additions & 0 deletions provider/textmagic/textmagic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package textmagic

import (
"context"
"fmt"
"log"
"strings"

textmagic "github.com/textmagic/textmagic-rest-go-v2/v2"
"github.com/messagebird/sachet"
)

type Config struct {
Username string `yaml:"username"`
APIKey string `yaml:"api_key"`
}

type TextMagic struct {
client *textmagic.APIClient
auth context.Context
}

func NewTextMagic(config Config) *TextMagic {
cfg := textmagic.NewConfiguration()
cfg.BasePath = "https://rest.textmagic.com"
client := textmagic.NewAPIClient(cfg)
auth := context.WithValue(context.Background(), textmagic.ContextBasicAuth, textmagic.BasicAuth{
UserName: config.Username,
Password: config.APIKey,
})
return &TextMagic{
client: client,
auth: auth,
}
}

func (tm *TextMagic) Send(message sachet.Message) error {
var err error = nil
switch message.Type {
case "", "text":
joinedPhones := strings.Join(message.To[:], ",")
_, _, err = tm.client.TextMagicApi.SendMessage(tm.auth, textmagic.SendMessageInputObject{
Text: message.Text,
Phones: joinedPhones,
From: message.From,
})
default:
return fmt.Errorf("unknown message type %s", message.Type)
}
return err
}

0 comments on commit 063e19f

Please sign in to comment.