GithubHelp home page GithubHelp logo

casdoor / go-sms-sender Goto Github PK

View Code? Open in Web Editor NEW
48.0 3.0 44.0 76 KB

A helper library to send SMS via 10+ channels, like Twilio, Amazon SNS, Azure ACS, etc.

Home Page: https://github.com/casdoor/casdoor

License: Apache License 2.0

Go 100.00%
sms go casdoor amazon-sns notification twilio amazon aws

go-sms-sender's Introduction

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.

go-sms-sender's People

Contributors

alrs avatar bsheepcoder avatar cofecatt avatar curlson avatar cutedrag0n avatar dacongda avatar denrianweiss avatar ebreak avatar eryajf avatar gtn1024 avatar hsluoyz avatar kofj avatar leo220yuyaodog avatar maratrixx avatar nomeguy avatar outofeastgate avatar seriouszyx avatar usherfall avatar waffle-frame avatar wzy9607 avatar yemosoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-sms-sender's Issues

SNS doesn't send code

My template: "Your code: %s"

Actual Result: "Your code: %s"

Expected result: "Your code: 123456", if code is 123456

I think here should be smth like it is on twilio:

        code, ok := param["code"]
	if !ok {
		return fmt.Errorf("missing parameter: msg code")
	}
	var err error

	bodyContent := fmt.Sprintf(c.template, code)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.