farhadi / node-smpp

SMPP client and server implementation in node.js
MIT License
417 stars 177 forks source link

Couldn't recieve SMS from SMSC #237

Closed elhananjair closed 7 months ago

elhananjair commented 1 year ago

Hello there I am trying to receive an SMS from SMSC which came from users. deliver_sm is working fine for delivery receipt, so I can see deliver_sm PDU for the sent SMS and I am using the same event to receive SMS from SMSC but nothing is here regarding the received SMS.

Can someone please help me with this? Thanks.

const smpp = require("smpp");

const session = smpp.connect(
  {
    url: "smpp://HOSTNAME:PORT",
    auto_enquire_link_period: 10000,
    debug: true,
  },
  function () {
    session.bind_receiver(
      {
        system_id: "ID",
        password: "PASSWORD",
        interface_version: "0x34",
        system_type: "0x00",
        addr_ton: "0x00",
        addr_npi: "0x00",
        address_range: "0x00",
      },
      function (pdu) {
        if (pdu.command_status === 0) {
          // Successfully bound
          console.log("Successfully Created Bound");
          session.submit_sm(
            {
              destination_addr: "destination_phone_number", // The destination phone number you want to send a message to
              source_addr_ton: 5, // Type of number of the source address
              source_addr_npi: 0, // 0 = Unknown, 1 = ISDN, 3 = Data, 4 = Telex, 5 = SMS, 6 = Radio, 7 = Fax, 8 = Videotelephony
              dest_addr_ton: 1, // Type of number of the destination phone number
              dest_addr_npi: 1, // 0 = Unknown, 1 = ISDN, 3 = Data, 4 = Telex, 5 = SMS, 6 = Radio, 7 = Fax, 8 = Videotelephony
              source_addr: "9923", // The Sender ID or Address, This will be displayed to the destination phone number
              registered_delivery: 1, // Set registered delivery (0 = no, 1 = yes)
              message_id: 1, // Message ID
              short_message: "Test message", // The message body, Replace it with the message you want to send.
              data_coding: 8,
              //message_payload: 'Optional message payload supports up to 65536 bytes'
            },
            function (pdu) {
              // Callback function for submit_sm
              if (pdu.command_status === 0) {
                // Successfully submitted
                // Message successfully sent
                console.log("message sent OK");
                return;
              } else {
                // Message failed to be sent
                console.log("message sending failed");
              }
              console.log("SOMETHING HAPPENED:", pdu); // Log the response
            },
          );
        }
        session.on("pdu", (pdu) => {
          if (pdu.command === "deliver_sm") {
            let msg = pdu.short_message.message;
            let from = pdu.source_addr.toString();
            let to = pdu.destination_addr.toString();
            console.log(`Deliver_SM received: ${msg} ${from} ${to}`);
            console.log("type of: " + typeof msg);
            session.send(pdu.response());
            // session.deliver_sm_resp({ sequence_number: pdu.sequence_number });
          } else {
            console.log("PDU Command is : " + pdu.command);
          }
        });
        session.on("error", (e) => {
          if (e.code === "ETIMEOUT") {
            console.log("Connection to SMS-C Timeout");
          } else if (e.code === "ECONNREFUSED") {
            console.log("Connection to SMS-C Refused");
          } else if (e.code === "EAI_AGAIN") {
            console.log("Connection to internet is lost!");
          } else {
            console.log(
              `There is something wrong connecting to SMS-C, Error code: ${e.code}`,
            );
          }
        });
      },
    );
  },
);
session.on("deliver_sm", (pdu) => {
  console.log(pdu); // Log the response
  if (pdu.esm_class === 4) {
    // Check if the message is a delivery receipt
    let shortMessage = pdu.short_message; // Get the message body
    console.log("Received DR: %s", shortMessage); // Log the response
    session.send(pdu.response()); // Send the response to the SMSC
  }
});
session.on("debug", function (type, msg, payload) {
  console.log({ type: type, msg: msg, payload: payload });
});
guicuton commented 1 year ago

The MO (received message) have esm_class different of 4. But despite of this you have to check with your broker if the connection that you use support 2-way message

elhananjair commented 1 year ago

@guicuton thanks, I found that the SMSC I am using only uses SOAP to send MO messages to my application, therefore it doesn't really relate to SMPP.

elhananjair commented 10 months ago

Hello @guicuton I tried bind_transciever and actually, I am getting MO messages but one issue here is when the user sends some keywords that trigger the subscription to my service I am not receiving those messages, in my case 'OK' is for subscription and 'Stop' is to unsubscribe.

Any ideas on this?

guicuton commented 10 months ago

Hello @guicuton I tried bind_transciever and actually, I am getting MO messages but one issue here is when the user sends some keywords that trigger the subscription to my service I am not receiving those messages, in my case 'OK' is for subscription and 'Stop' is to unsubscribe.

Any ideas on this?

Usually the SMSC only will send the MO to you as an answer of a MT.

If your user is sending, for example "OK" (doesn't matter the word) for your shortcode without being an answer from a previous MT that you sent to him this message will reach your SMSC provider but they will not know to which client they have to reflect the MO, which means that you'll not receive the message.

To work with keywords you have to check with your SMSC provider if they support work with keywords and if they have the keyword available for you.

Once they have settled up for you the shortcode and the keyword, every time that someone sends "ok" for your shortcode 29999 you will receive the message on deliver_sm event

elhananjair commented 10 months ago

If your user is sending, for example "OK" (doesn't matter the word) for your shortcode without being an answer from a previous MT that you sent to him this message will reach your SMSC provider but they will not know to which client they have to reflect the MO, which means that you'll not receive the message.

But as I said earlier, I am receiving MO messages already (with the bind_transceiver method) here is an example I am outputting sending SMS from my phone:

Oct 20 15:38:35 fedora node[11193]: { message: 'Hey ' }
Oct 20 15:39:03 fedora node[11193]: { message: 'Hey ' }
Oct 20 15:39:25 fedora node[11193]: { message: 'Hey ' }
Oct 20 15:39:37 fedora node[11193]: { message: 'Test' }

But as I send Ok and Stop it won't send to my application, that's what confused me.

To work with keywords you have to check with your SMSC provider if they support work with keywords and if they have the keyword available for you.

The keywords are 'OK' for subscription and 'Stop' for unsubscribe. If they subscribe with OK they will receive SMS sent from my application.

Once they have settled up for you the shortcode and the keyword, every time that someone sends "ok" for your shortcode 29999 you will receive the message on deliver_sm event

I have a shortcode for both MT and MO, currently, I am only using an MT shortcode to send SMS, but I have no clue where I need to use the MO shortcode in my code.

elhananjair commented 10 months ago

@guicuton any ideas on my reply?

@farhadi @juliangut any suggestions on this issue?

Thanks

elhananjair commented 7 months ago

Well, finally it seems the SMSC is sending the MO messages through HTTP post request with an XML request body, that way I am parsing the request body to receive the details.