forked from messagebird/sachet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca9ca58
commit 063e19f
Showing
5 changed files
with
96 additions
and
0 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
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,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' | ||
``` |
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,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 | ||
} |