go_sms_sender

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: Apache-2.0 Imports: 24 Imported by: 0

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

Demo

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)
	}
}

License

This project is licensed under the Apache 2.0 license.

Documentation

Index

Constants

View Source
const (
	Aliyun       = "Aliyun SMS"
	TencentCloud = "Tencent Cloud SMS"
	VolcEngine   = "Volc Engine SMS"
	Huyi         = "Huyi SMS"
	HuaweiCloud  = "Huawei Cloud SMS"
	Twilio       = "Twilio SMS"
	SmsBao       = "SmsBao SMS"
	MockSms      = "Mock SMS"
	SUBMAIL      = "SUBMAIL SMS"
	GCCPAY       = "GCCPAY SMS"
)
View Source
const AUTH_HEADER_VALUE = "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\""
View Source
const WSSE_HEADER_FORMAT = "UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\""

Variables

This section is empty.

Functions

func GetMd5String

func GetMd5String(s string) string

func Md5

func Md5(str string) string

func RandStringBytesCrypto

func RandStringBytesCrypto(n int) (string, error)

Types

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

type AliyunResult struct {
	RequestId string
	Message   string
}

type GCCPAYClient

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

func GetGCCPAYClient

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

func (*GCCPAYClient) SendMessage

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

type HuaweiClient

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

func GetHuaweiClient

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

func (*HuaweiClient) SendMessage

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

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

func GetHuyiClient

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

func (*HuyiClient) SendMessage

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

type Mocker

type Mocker struct{}

func NewMocker

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

func (*Mocker) SendMessage

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

type SmsBaoClient

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

func GetSmsbaoClient

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

func (*SmsBaoClient) SendMessage

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 SubmailClient

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

func GetSubmailClient

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

func (*SubmailClient) SendMessage

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

type SubmailResult

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

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

func GetTwilioClient

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

func (*TwilioClient) SendMessage

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 VolcClient

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

func GetVolcClient

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

func (*VolcClient) SendMessage

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