go_sms_sender

package module
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2024 License: Apache-2.0 Imports: 37 Imported by: 1

README

go-sms-sender

Go Report Card Go Go Reference GitHub release (latest SemVer)

This is a powerful open-source library for sending SMS message, which will help you to easily integrate with the popular SMS providers. And it has been applied to Casdoor, if you still don’t know how to use it after reading README.md, you can refer to it.

We support the following SMS providers, welcome to contribute.

Installation

Use go get to install:

go get github.com/casdoor/go-sms-sender

How to use

Create Client

Different SMS providers need to provide different configuration, but we support a unit API as below to init and get the SMS client.

func NewSmsClient(provider string, accessId string, accessKey string, sign string, template string, other ...string) (SmsClient, error)
  • provider the name of SMS provider, such as Aliyun SMS
  • accessId
  • accessKey
  • sign the sign name
  • template the template code
  • other other configuration
Send Message

After initializing the SMS client, we can use the following API to send message.

SendMessage(param map[string]string, targetPhoneNumber ...string) error
  • param the parameters in the SMS template, such as 6 random numbers
  • targetPhoneNumber the receivers, such as +8612345678910

Example

Twilio

Please get necessary information from Twilio console

package main

import "github.com/casdoor/go-sms-sender"

func main() {
	client, err := go_sms_sender.NewSmsClient(go_sms_sender.Twilio, "ACCOUNT_SID", "AUTH_TOKEN", "", "TEMPLATE_CODE")
	if err != nil {
		panic(err)
	}

	params := map[string]string{}
	params["code"] = "123456"
	phoneNumer := "+8612345678910"
	err = client.SendMessage(params, phoneNumer)
	if err != nil {
		panic(err)
	}
}
Aliyun

Before you begin, you need to sign up for an Aliyun account and retrieve your Credentials.

package main

import "github.com/casdoor/go-sms-sender"

func main() {
	client, err := go_sms_sender.NewSmsClient(go_sms_sender.Aliyun, "ACCESS_KEY_ID", "ACCESS_KEY_SECRET", "SIGN_NAME", "TEMPLATE_CODE")
	if err != nil {
		panic(err)
	}

	params := map[string]string{}
	params["code"] = "473956"
	phoneNumer := "+8612345678910"
	err = client.SendMessage(params, phoneNumer)
	if err != nil {
		panic(err)
	}
}
Tencent Cloud
package main

import "github.com/casdoor/go-sms-sender"

func main() {
	client, err := go_sms_sender.NewSmsClient(go_sms_sender.TencentCloud, "secretId", "secretKey", "SIGN_NAME", "TEMPLATE_CODE", "APP_ID")
	if err != nil {
		panic(err)
	}

	params := map[string]string{}
	params["0"] = "473956"
	phoneNumer := "+8612345678910"
	err = client.SendMessage(params, phoneNumer)
	if err != nil {
		panic(err)
	}
}
Netgsm
  • yourAccessId: is KullaniciAdi
  • yourAccessKey: is Sifre
  • yourSign: is Baslik
package main

import "github.com/casdoor/go-sms-sender"

func main() {
	client, err := go_sms_sender.NewSmsClient(go_sms_sender.Netgsm, "yourAccessId", "yourAccessKey", "yourSign", "yourTemplate")
	if err != nil {
		panic(err)
	}

	params := map[string]string{}
	params["param1"] = "value1"
	params["param2"] = "value2"
	phoneNumer := "+8612345678910"
	err = client.SendMessage(params, phoneNumer)
	if err != nil {
		panic(err)
	}
}
Oson Sms
  • senderId: is login
  • secretAccessKey: is hash
  • signName: is from
  • templateCode: is message
package main

func main() {
	client, err := go_sms_sender.NewSmsClient(go_sms_senderOsonSms, "senderId", "secretAccessKey", "signName", "templateCode")
	if err != nil {
		panic(err)
	}

	params := map[string]string{}
	params["code"] = "123456"
	phoneNumer := "+992123456789"
	err = client.SendMessage(params, phoneNumer)
	if err != nil {
		panic(err)
	}
}
Running Tests

To run tests for the go-sms-sender library, navigate to the root folder of the project in your terminal and execute the following command:

go test -v ./...

you can modify mock_test.go file to mock an other tests

License

This project is licensed under the Apache 2.0 license.

Documentation

Index

Constants

View Source
const (
	Twilio       = "Twilio SMS"
	AmazonSNS    = "Amazon SNS"
	AzureACS     = "Azure ACS"
	Msg91        = "Msg91 SMS"
	GCCPAY       = "GCCPAY SMS"
	Infobip      = "Infobip SMS"
	SUBMAIL      = "SUBMAIL SMS"
	SmsBao       = "SmsBao SMS"
	Aliyun       = "Aliyun SMS"
	TencentCloud = "Tencent Cloud SMS"
	BaiduCloud   = "Baidu Cloud SMS"
	VolcEngine   = "Volc Engine SMS"
	HuaweiCloud  = "Huawei Cloud SMS"
	UCloud       = "UCloud SMS"
	Huyi         = "Huyi SMS"
	MockSms      = "Mock SMS"
	Netgsm       = "Netgsm SMS"
	OsonSms      = "OSON SMS"
)
View Source
const (
	WSSE_HEADER_FORMAT = "UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\""
	AUTH_HEADER_VALUE  = "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\""
)

Variables

This section is empty.

Functions

func GetMd5String added in v0.0.6

func GetMd5String(s string) string

func Md5 added in v0.9.0

func Md5(str string) string

func RandStringBytesCrypto added in v0.9.0

func RandStringBytesCrypto(n int) (string, error)

Types

type ACSClient added in v0.11.0

type ACSClient struct {
	AccessToken string
	Endpoint    string
	Message     string
	Sender      string
}

func GetACSClient added in v0.11.0

func GetACSClient(accessToken string, message string, other []string) (*ACSClient, error)

func (*ACSClient) SendMessage added in v0.11.0

func (a *ACSClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type AliyunClient

type AliyunClient struct {
	// contains filtered or unexported fields
}

func GetAliyunClient

func GetAliyunClient(accessId string, accessKey string, sign string, template string) (*AliyunClient, error)

func (*AliyunClient) SendMessage

func (c *AliyunClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type AliyunResult added in v0.6.0

type AliyunResult struct {
	RequestId string
	Message   string
}

type AmazonSNSClient added in v0.11.0

type AmazonSNSClient struct {
	// contains filtered or unexported fields
}

func GetAmazonSNSClient added in v0.11.0

func GetAmazonSNSClient(accessKeyID string, secretAccessKey string, template string, region []string) (*AmazonSNSClient, error)

func (*AmazonSNSClient) SendMessage added in v0.11.0

func (a *AmazonSNSClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type BaiduClient added in v0.8.0

type BaiduClient struct {
	// contains filtered or unexported fields
}

func GetBceClient added in v0.8.0

func GetBceClient(accessId, accessKey, sign, template string, endpoint []string) (*BaiduClient, error)

func (*BaiduClient) SendMessage added in v0.8.0

func (c *BaiduClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type Destination added in v0.9.0

type Destination struct {
	To string `json:"to"`
}

type GCCPAYClient added in v0.9.0

type GCCPAYClient struct {
	// contains filtered or unexported fields
}

func GetGCCPAYClient added in v0.9.0

func GetGCCPAYClient(clientname string, secret string, template string) (*GCCPAYClient, error)

func (*GCCPAYClient) SendMessage added in v0.9.0

func (c *GCCPAYClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type HuaweiClient added in v0.2.0

type HuaweiClient struct {
	// contains filtered or unexported fields
}

func GetHuaweiClient added in v0.2.0

func GetHuaweiClient(accessId string, accessKey string, sign string, template string, other []string) (*HuaweiClient, error)

func (*HuaweiClient) SendMessage added in v0.2.0

func (c *HuaweiClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

SendMessage https://support.huaweicloud.com/intl/en-us/devg-msgsms/sms_04_0012.html

type HuyiClient added in v0.0.6

type HuyiClient struct {
	// contains filtered or unexported fields
}

func GetHuyiClient added in v0.0.6

func GetHuyiClient(appId string, appKey string, template string) (*HuyiClient, error)

func (*HuyiClient) SendMessage added in v0.0.6

func (hc *HuyiClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type InfobipClient added in v0.9.0

type InfobipClient struct {
	// contains filtered or unexported fields
}

func GetInfobipClient added in v0.9.0

func GetInfobipClient(sender string, apiKey string, template string, baseUrl []string) (*InfobipClient, error)

func (*InfobipClient) SendMessage added in v0.9.0

func (c *InfobipClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type InfobipConfigService added in v0.9.0

type InfobipConfigService struct {
	// contains filtered or unexported fields
}

type Message added in v0.9.0

type Message struct {
	From         string        `json:"from"`
	Destinations []Destination `json:"destinations"`
	Text         string        `json:"text"`
}

type MessageData added in v0.9.0

type MessageData struct {
	Messages []Message `json:"messages"`
}

type Mocker added in v0.3.0

type Mocker struct{}

func NewMocker added in v0.3.0

func NewMocker(accessId, accessKey, sign, templateId string, smsAccount []string) (*Mocker, error)

func (*Mocker) SendMessage added in v0.3.0

func (m *Mocker) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type Msg91Client added in v0.10.0

type Msg91Client struct {
	// contains filtered or unexported fields
}

func GetMsg91Client added in v0.10.0

func GetMsg91Client(senderId string, authKey string, templateId string) (*Msg91Client, error)

func (*Msg91Client) SendMessage added in v0.10.0

func (m *Msg91Client) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type NetgsmClient added in v0.13.0

type NetgsmClient struct {
	// contains filtered or unexported fields
}

func GetNetgsmClient added in v0.13.0

func GetNetgsmClient(accessId, accessKey, sign, template string) (*NetgsmClient, error)

func (*NetgsmClient) SendMessage added in v0.13.0

func (c *NetgsmClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type NetgsmResponse added in v0.13.0

type NetgsmResponse struct {
	Code  string `xml:"main>code"`
	JobID string `xml:"main>jobID"`
	Error string `xml:"main>error"`
}

type OsonClient added in v0.20.0

type OsonClient struct {
	Endpoint         string
	SenderID         string
	SecretAccessHash string
	Sign             string
	Message          string
}

func GetOsonClient added in v0.20.0

func GetOsonClient(senderId, secretAccessHash, sign, message string) (*OsonClient, error)

func (*OsonClient) SendMessage added in v0.20.0

func (c *OsonClient) SendMessage(param map[string]string, targetPhoneNumber ...string) (err error)

type OsonResponse added in v0.20.0

type OsonResponse struct {
	Status        string    // ok
	Timestamp     time.Time // 2017-07-07 16:58:12
	TxnId         string    // f89xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxe0b
	MsgId         uint      // 40127
	SmscMsgId     string    // 45f22479
	SmscMsgStatus string    // success
	SmscMsgParts  string    // 1
}

type SmsBaoClient added in v0.5.0

type SmsBaoClient struct {
	// contains filtered or unexported fields
}

func GetSmsbaoClient added in v0.5.0

func GetSmsbaoClient(username string, apikey string, sign string, template string, other []string) (*SmsBaoClient, error)

func (*SmsBaoClient) SendMessage added in v0.5.0

func (c *SmsBaoClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type SmsClient

type SmsClient interface {
	SendMessage(param map[string]string, targetPhoneNumber ...string) error
}

func NewSmsClient

func NewSmsClient(provider string, accessId string, accessKey string, sign string, template string, other ...string) (SmsClient, error)

type SmsService added in v0.9.0

type SmsService struct {
	// contains filtered or unexported fields
}

type SubmailClient added in v0.6.0

type SubmailClient struct {
	// contains filtered or unexported fields
}

func GetSubmailClient added in v0.6.0

func GetSubmailClient(appid string, signature string, project string) (*SubmailClient, error)

func (*SubmailClient) SendMessage added in v0.6.0

func (c *SubmailClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type SubmailResult added in v0.6.0

type SubmailResult struct {
	Status string `json:"status"`
	Code   int    `json:"code"`
	Msg    string `json:"msg"`
}

type TencentClient

type TencentClient struct {
	// contains filtered or unexported fields
}

func GetTencentClient

func GetTencentClient(accessId string, accessKey string, sign string, templateId string, appId []string) (*TencentClient, error)

func (*TencentClient) SendMessage

func (c *TencentClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type TwilioClient added in v0.4.0

type TwilioClient struct {
	// contains filtered or unexported fields
}

func GetTwilioClient added in v0.4.0

func GetTwilioClient(accessId string, accessKey string, template string) (*TwilioClient, error)

func (*TwilioClient) SendMessage added in v0.4.0

func (c *TwilioClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

SendMessage targetPhoneNumber[0] is the sender's number, so targetPhoneNumber should have at least two parameters

type UcloudClient added in v0.9.0

type UcloudClient struct {
	ProjectId  string
	PrivateKey string
	PublicKey  string
	Sign       string
	Template   string
	// contains filtered or unexported fields
}

func GetUcloudClient added in v0.9.0

func GetUcloudClient(publicKey string, privateKey string, sign string, template string, projectId []string) (*UcloudClient, error)

func (*UcloudClient) SendMessage added in v0.9.0

func (c *UcloudClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

type VolcClient added in v0.0.3

type VolcClient struct {
	// contains filtered or unexported fields
}

func GetVolcClient added in v0.0.3

func GetVolcClient(accessId, accessKey, sign, templateId string, smsAccount []string) (*VolcClient, error)

func (*VolcClient) SendMessage added in v0.0.3

func (c *VolcClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL