sms

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2023 License: MIT Imports: 5 Imported by: 0

README

go report card

Sms

Walle/sms

基于 Gsms 添加修改的go版本短信发送

特点

  1. 支持目前市面多家服务商
  2. 一套写法兼容所有平台
  3. 简单配置即可灵活增减服务商
  4. 内置多种服务商轮询策略、支持自定义轮询策略
  5. 统一的返回值格式,便于日志与监控
  6. 自动轮询选择可用的服务商

平台支持

安装

go get -u github.com/maiqingqiang/gsms

使用

package main

import (
	"github.com/maiqingqiang/gsms"
	"github.com/maiqingqiang/gsms/core"
	"github.com/maiqingqiang/gsms/gateways/yunpian"
	"log"
)

func main() {
	g := sms.New(
		[]core.Gateway{
			&yunpian.Gateway{
				ApiKey:    "f4c1c41f48120eb311111111111097",
				Signature: "【默认签名】",
			},
		},
		sms.WithDefaultGateway([]string{
			yunpian.NAME,
		}),
	)

	results, err := g.Send(18888888888, &core.Message{
		Template: "SMS_00000001",
		Data: map[string]string{
			"code": "521410",
		},
	})

	if err != nil {
		log.Fatalf("发送失败 %+v", err)
	}

	log.Printf("发送成功 %+v", results)
}

短信内容

由于使用多网关发送,所以一条短信要支持多平台发送,每家的发送方式不一样,但是我们抽象定义了以下公用属性:

  • Content 文字内容,使用在像云片类似的以文字内容发送的平台
  • Template 模板 ID,使用在以模板ID来发送短信的平台
  • Data 模板变量,使用在以模板ID来发送短信的平台

所以,在使用过程中你可以根据所要使用的平台定义发送的内容。

client.Send(18888888888, &core.Message{
    Template: "SMS_00000001",
    Data: map[string]string{
        "code": "521410",
    },
})

client.Send(18888888888, &core.Message{
    Content: "您的验证码为: 6379",
})

闭包方式

client.Send(18888888888, &core.Message{
    Template: func(gateway core.GatewayInterface) string {
        if gateway.Name() == aliyun.NAME {
            return "SMS_271385117"
        }
        return "5532044"
    },
    Data: func(gateway core.GatewayInterface) map[string]string {
        if gateway.Name() == aliyun.NAME {
            return map[string]string{
                "code": "1111",
            }
        }
        return map[string]string{
            "code": "6379",
        }
    },
})

发送网关

client.Send(18888888888, &core.Message{
    Template: "5532044",
    Data: map[string]string{
        "code": "6379",
    },
}, yunpian.NAME, aliyun.NAME)

自定义网关

只需要实现 core.GatewayInterface 接口即可,例如:

场景发送


var _ core.MessageInterface = (*OrderPaidMessage)(nil)

type OrderPaidMessage struct {
	OrderNo string
}

func (o *OrderPaidMessage) Gateways() ([]string, error) {
	return []string{yunpian.NAME}, nil
}

func (o *OrderPaidMessage) Strategy() (core.StrategyInterface, error) {
	return nil, nil
}

func (o *OrderPaidMessage) GetContent(gateway core.GatewayInterface) (string, error) {
	return fmt.Sprintf("您的订单:%s, 已经完成付款", o.OrderNo), nil
}

func (o *OrderPaidMessage) GetTemplate(gateway core.GatewayInterface) (string, error) {
	return "5532044", nil
}

func (o *OrderPaidMessage) GetData(gateway core.GatewayInterface) (map[string]string, error) {
	return map[string]string{
		"code": "6379",
	}, nil
}

func (o *OrderPaidMessage) GetType(gateway core.GatewayInterface) (string, error) {
	return core.TextMessage, nil
}

client.Send(18888888888, &OrderPaidMessage{OrderNo: "1234"})

版权说明

该项目签署了 MIT 授权许可,详情请参阅 LICENSE

鸣谢

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Sms)

set sms option

type Sms

type Sms struct {
	// default gateways
	DefaultGateways []string

	//Timeout default 5 seconds
	Timeout time.Duration

	// Gateways
	Gateways map[string]core.GatewayInterface

	Strategy core.StrategyInterface
	// contains filtered or unexported fields
}

Sms

func New

func New(gateways []core.GatewayInterface, options ...Option) *Sms

New sms

func (*Sms) Gateway

func (s *Sms) Gateway(name string) (core.GatewayInterface, error)

Gateway Get gateway by name

func (*Sms) Send

func (s *Sms) Send(ctx context.Context, to interface{}, message core.MessageInterface, gateways ...string) ([]*core.Result, error)

Directories

Path Synopsis
gateways

Jump to

Keyboard shortcuts

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