0.0.11 • Published 3 years ago

node-smgp v0.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

a tool for chinatelecom smgp.

Supports Node.js >= 14.

Quick Start

Install

$ npm install node-smgp

Client Example

Connnect Config

const config = {
  host: string;
  port: number;
  clientID: string;
  secret: string;
  spId: string;
  serviceId: string;
  srcId: string;
}

Create Connection

import Socket from 'node-smgp';

const SMGPSocket = new Socket({
  host: '127.0.0.1',
  port: 7789,
  clientID: 'abcdef',
  secret: 'abcdefg',
  spId: '10691234',
  serviceId: 'smgpservice',
  srcId: '10691234',
});

Exit

SMGPSocket.on('exit', exitMsg => {
  console.log(exitMsg);
});

TimeOut

send 3 times

SMGPSocket.on('timeout', (phone, content) => {
  console.log(phone, content);
});

Deliver

SMGPSocket.on('deliver', (Msg, callback) => {
  if (callback) callback();
  const { header, body } = Msg;
  const { SequenceID } = header;
  const { MsgID, IsReport, MsgFormat, SrcTermID, DestTermID, MsgLength, MsgContent } = body;
  switch (IsReport) {
    //状态报告
    case 1:
      db.inserte({ SequenceID, MsgID, MsgFormat, SrcTermID, DestTermID, MsgLength, MsgContent });
    //上行消息
    default:
      console.log(MsgContent);
  }
});

Send SMS

SMGPSocket.sendSms('13301171412', '【京东】您的验证码为:123456');

Server Example

Create server

import Socket from 'node-smgp';

const server = new Server({
  host: '127.0.0.1',
  port: 9000,
  // process login
  Login: (loginMsg: { header: IHeader; body: ILogin }): ILogin_Resp => {
    console.log(loginMsg);
    return { Status: 0, AuthenticatorServer: '1', ServerVersion: 0x03 };
  },
  // process submit
  Submit: (submitMsg: { header: IHeader; body: ISubmit }): ISubmit_Resp => {
    // console.log(submitMsg);

    return { Status: 0, MsgID: Date.now().toString() };
  },

  // process deliver
  Deliver: (deliverMsg: { header: IHeader; body: IDeliver_Resp }) => {
    console.log(deliverMsg);
  },
});

server.start();
server.on('error', error => {
  console.log(error);
});

deliver

server.deliver({ 
  MsgID: '123', 
  IsReport: 1, 
  MsgFormat: 8, 
  RecvTime: Date.now().toString(), 
  SrcTermID: '10691234', 
  DestTermID: '13301112222', 
  MsgLength: 12, 
  MsgContent: '123', 
  Reserve: '' 
})

Questions & Suggestions

Please open an issue here.

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago