gosms

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2019 License: MIT Imports: 7 Imported by: 0

README

GOSMS

Build Status codecov Go Report Card GoDoc

A Golang client library over the Restful APIs of www.sms.ir. I changed some of the APIs of this service to be more intuitive.

In the table below, I indicate which modules and methods of the SMS service implemented in this library.

Modules APIs
UserInfo
  • [x] GetToken
  • [x] Credit
  • [ ] SMSLine
Send-Receive
  • [ ] MessageSend
  • [ ] MessageSend (ReportByDate)
  • [ ] MessageSend (ReportById)
  • [ ] MessageSend (ReportByBachkey)
  • [ ] ReceiveMessage (ByLastID)
  • [ ] ReceiveMessage (ByDate)
CustomerClub
  • [ ] CustomerClubContact (AddContact)
  • [ ] CustomerClubContact (UpdateContact)
  • [ ] CustomerClubContact (GetCategories)
  • [ ] CustomerClubContact (GetContactsByCategory&ById)
  • [ ] CustomerClubContact (GetAllContactsByPageID)
  • [ ] CustomerClub (Send)
  • [ ] CustomerClub (AddContact&Send)
  • [ ] CustomerClub (SendToCategories)
  • [ ] CustomerClub (GetSendMessagesByPagination)
  • [ ] CustomerClub (GetSendMessagesByPaginationAndLastId)
  • [ ] CustomerClub (DeleteContactCustomerClub)
Verification
  • [x] VerificationCode
  • [x] UltraFastSend
  • [ ] MessageReport

Contribution

Any contribution is welcome to this repo. If you find an issue, please report that in the Issues section. Beyond that, if you like to code in this repo, I appreciate that.

Documentation

Index

Examples

Constants

View Source
const DefaultBulkURL = "https://restfulsms.com/api"

DefaultBulkURL is used to send requests to SMS provider by default.

View Source
const TokenTimeOut = 20 * time.Minute

TokenTimeOut indicates refresh time of the token for accessing APIs.

Variables

This section is empty.

Functions

This section is empty.

Types

type BulkSMS

type BulkSMS struct {
	BaseURL string
	Token   TokenProvider
}

func (*BulkSMS) GetCredit

func (b *BulkSMS) GetCredit() (int, error)

GetCredit fetches the amount of the SMS count that remains on the account. It uses the token that provides by the Token.Get() method.

Example
package main

import (
	"github.com/amirhosseinab/gosms"
	"log"
)

func main() {
	tp := gosms.NewToken(gosms.Config{
		APIKey:    "YOUR_API_KEY",
		SecretKey: "YOUR_SECRET_KEY",
	})

	client := gosms.NewBulkSMSClient(tp, gosms.DefaultBulkURL)

	credit, err := client.GetCredit()
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Your credit is: %d", credit)
}
Output:

func (*BulkSMS) SendByTemplate

func (b *BulkSMS) SendByTemplate(mobile string, templateId int, params map[string]string) (string, error)

SendByTemplate sends a bunch of key-value pair of data with a provided template(TemplateId) to the given mobile number.

Example
package main

import (
	"github.com/amirhosseinab/gosms"
	"log"
)

func main() {
	token := gosms.NewToken(gosms.Config{
		APIKey:    "YOUR_API_KEY",
		SecretKey: "YOUR_SECRET_KEY",
	})

	client := gosms.NewBulkSMSClient(token, gosms.DefaultBulkURL)

	vId, err := client.SendByTemplate("09121234567", 1234, map[string]string{"VerificationCode": "9999"})
	if err != nil {
		log.Fatal(err)
		return
	}

	log.Printf("Verification Id is %s", vId)
}
Output:

func (*BulkSMS) SendVerificationCode

func (b *BulkSMS) SendVerificationCode(mobile, code string) (string, error)

SendVerificationCode sends a value(code) with default message template to the provided mobile number.

Example
package main

import (
	"github.com/amirhosseinab/gosms"
	"log"
)

func main() {
	token := gosms.NewToken(gosms.Config{
		APIKey:    "YOUR_API_KEY",
		SecretKey: "YOUR_SECRET_KEY",
	})

	client := gosms.NewBulkSMSClient(token, gosms.DefaultBulkURL)

	vId, err := client.SendVerificationCode("09121234567", "123456")
	if err != nil {
		log.Fatal(err)
		return
	}

	log.Printf("Verification Id is %s", vId)
}
Output:

type BulkSMSProvider

type BulkSMSProvider interface {
	GetCredit() (int, error)
	SendVerificationCode(mobile, code string) (string, error)
	SendByTemplate(mobile string, templateId int, params map[string]string) (string, error)
}

BulkSMSProvider exposes the methods of bulk SMS system.

func NewBulkSMSClient

func NewBulkSMSClient(token TokenProvider, url string) BulkSMSProvider

NewBulkSMSClient creates a value that handles all requests for bulk SMS system.

type Config

type Config struct {
	BaseURL      string
	APIKey       string
	SecretKey    string
	DisableCache bool
}

Config holds the data that is required for constructing Token.

type Token

type Token struct {
	Config Config
}

Token handles the requests for providing token

func (*Token) Get

func (t *Token) Get() (string, error)

Get method fetches token from the server. It is thread-safe and handles the caching mechanism by default to prevent unnecessary requests.

type TokenProvider

type TokenProvider interface {
	Get() (string, error)
}

TokenProvider is used to fetch the token from the server.

func NewToken

func NewToken(config Config) TokenProvider

NewToken provides value for fetching token from the server.

Jump to

Keyboard shortcuts

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