smpp node, whenever I send an SMS, do I need to create a new connection?

66 views Asked by At

how are you, I'm new here, so I may be asking a question that may have already been answered, so if that's the case, I'm sorry.

I'm working with the node smpp library and I have a question regarding sessions, every time I send an SMS do I need to create the session again? Because I couldn't quite understand the flow, and if I don't need to create a new connection, would anyone have an example to share? I'll leave a print of my code. Thank you for the help

Every time I need to send a new SMS I end up creating a new session, because the way I found the examples it's not very clear

import { Injectable } from '@nestjs/common';
import { SmsService } from 'src/sms/sms.service';

@Injectable()
export class SessionSmppService {

  constructor(private smsService: SmsService) { };
  private session;

  deleteSms(idSms) {
    this.smsService.deleteSmsSent(idSms);
  }

  insertSmsSent(pdu, sms) {
    let isOperadora = pdu.message_id;
    if (pdu.command_status == 0) {
      console.log('Message successfully sent: ', pdu);
      this.smsService.insertSmsSent(sms, "DELIVERED", isOperadora)

    } else {
      console.log('Message Error sent: ', pdu);
      this.smsService.insertSmsSent(sms, "NOT_DELIVERED", isOperadora)
    }
  }

  submitSms(pdu, session, sms) {

    if (pdu.command_status == 0) {
      for (let index = 0; index < sms.length; index++) {

        session.submit_sm({
          destination_addr: sms[index].num_tel,
          short_message: sms[index].msg

        },
          pdu => this.insertSmsSent(pdu, sms[index]), 
          () => this.deliverSm(this.session));

        this.deleteSms(sms[index].id_sms)
      }
    } else {
      console.log('Error bind_transceiver', pdu)
    }

  }

  deliverSm(session) {

    session.on('pdu', function (pdu) {
      if (pdu.command == 'deliver_sm') {
        var toNumber = pdu.destination_addr.toString();

        var text = '';
        if (pdu.short_message && pdu.short_message.message) {
          text = pdu.short_message.message;
        }

        console.log('SMS ' + ' -> ' + toNumber + ': ' + text);
        session.deliver_sm_resp({ sequence_number: pdu.sequence_number });
      }
    })

  }

  sendSms(sms: any) {

    let url = process.env.URL_SMPPS;
    let systemId = process.env.SYSTEM_ID;
    let password = process.env.PASSWORD;

    let smpp = require('smpp');

    this.session = smpp.connect({
      url: url,
      auto_enquire_link_period: 10000,
      debug: true,
      registered_delivery: 1
    }, () => this.session.on('debug', function (type, msg, payload) {
      console.log({ type: type, msg: msg, payload: payload });
    }))

    this.session.on('connect', () => {

      this.session.bind_transceiver({
        system_id: systemId,
        password: password,
      },
        pdu => this.submitSms(pdu, this.session, sms),
        () => this.deliverSm(this.session),
        function () {
          this.session.on('error', error => {
            console.log('smpp error', error)
          });
        })
    })
  }
}
0

There are 0 answers