Skip to content

Commit

Permalink
Merge pull request #123 from EM4D/master
Browse files Browse the repository at this point in the history
Add Melipayamak as a new provider
  • Loading branch information
marcel corso gonzalez authored Dec 20, 2022
2 parents 05b289f + 9806d7b commit 65fcbcb
Show file tree
Hide file tree
Showing 4 changed files with 80 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 @@ -18,6 +18,7 @@ import (
"github.com/messagebird/sachet/provider/kavenegar"
"github.com/messagebird/sachet/provider/mailruim"
"github.com/messagebird/sachet/provider/mediaburst"
"github.com/messagebird/sachet/provider/melipayamak"
"github.com/messagebird/sachet/provider/messagebird"
"github.com/messagebird/sachet/provider/nexmo"
"github.com/messagebird/sachet/provider/nowsms"
Expand Down Expand Up @@ -75,6 +76,7 @@ var config struct {
Ghasedak ghasedak.Config
Sfr sfr.Config
TextMagic textmagic.Config
Melipayamak melipayamak.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 @@ -26,6 +26,7 @@ import (
"github.com/messagebird/sachet/provider/kavenegar"
"github.com/messagebird/sachet/provider/mailruim"
"github.com/messagebird/sachet/provider/mediaburst"
"github.com/messagebird/sachet/provider/melipayamak"
"github.com/messagebird/sachet/provider/messagebird"
"github.com/messagebird/sachet/provider/nexmo"
"github.com/messagebird/sachet/provider/nowsms"
Expand Down Expand Up @@ -147,6 +148,8 @@ func providerByName(name string) (sachet.Provider, error) {
return sfr.NewSfr(config.Providers.Sfr), nil
case "textmagic":
return textmagic.NewTextMagic(config.Providers.TextMagic), nil
case "melipayamak":
return melipayamak.NewMelipayamak(config.Providers.Melipayamak), nil
}

return nil, fmt.Errorf("%s: Unknown provider", name)
Expand Down
9 changes: 9 additions & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ providers:
textmagic:
username: 'username'
api_key: 'TEXTMAGIC_API_KEY'
melipayamak:
username: '###'
password: '###'
endpoint: 'https://rest.payamak-panel.com/api/SendSMS/SendSMS'

templates:
- telegram.tmpl
Expand Down Expand Up @@ -152,3 +156,8 @@ receivers:
to:
- '09012345679'
- '09123456789'
- name: 'melipayamak'
provider: 'melipayamak'
to:
- '09123456789'
from: '50004000000000'
66 changes: 66 additions & 0 deletions provider/melipayamak/melipayamak.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package melipayamak

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/messagebird/sachet"
)

type Config struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
From string `yaml:"from"`
Endpoint string `yaml:"endpoint"`
}

var _ (sachet.Provider) = (*Melipayamak)(nil)

type Melipayamak struct {
Config
HTTPClient *http.Client
}

func NewMelipayamak(config Config) *Melipayamak {
return &Melipayamak{
config,
&http.Client{Timeout: time.Second * 20},
}
}

func (mp *Melipayamak) Send(message sachet.Message) error {

Payload := map[string]string{
"username": mp.Username,
"password": mp.Password,
"to": strings.Join(message.To, ","),
"from": message.From,
"text": message.Text,
}
data, _ := json.Marshal(Payload)
request, err := http.NewRequest("POST", mp.Endpoint, bytes.NewBuffer(data))
if err != nil {
return err
}
request.Header.Set("content-type", "application/json")
request.Header.Set("User-Agent", "Sachet")
response, err := mp.HTTPClient.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(response.Body)
return fmt.Errorf(
"SMS sending failed. HTTP status code: %d, Response body: %s",
response.StatusCode,
body,
)
}
return nil
}

0 comments on commit 65fcbcb

Please sign in to comment.