alexandr-mironov / php8-smpp

SMPP Client (v 3.4) on PHP8
7 stars 8 forks source link

DLT Tag #10

Closed meetshamee closed 1 year ago

meetshamee commented 1 year ago

We are from India. We cannot send sms without DLT / PEID. No idea how DLT is tagged on this. Can you help?

alexandr-mironov commented 1 year ago

Hello, it seems like you need to add two tags for messages (depended on your SMS gateway provider)

something like this:

<?php

declare(strict_types=1);

namespace app\components\Sms;

use Exception;
use smpp\{Address, Client as SmppClient, Smpp, Tag, transport\Socket};

class SmsBuilder
{
    /** @var string 11 chars limit */
    public const DEFAULT_SENDER = 'example';

    protected Socket $transport;

    protected SmppClient $smppClient;

    protected bool $debug = false;

    protected Address $from;

    protected Address $to;

    protected string $login;

    protected string $password;

    protected ?array $tags = null;

    /**
     * SmsBuilder constructor.
     *
     * @param string $address SMSC IP
     * @param int $port SMSC port
     * @param string $login
     * @param string $password
     * @param int $timeout timeout of reading PDU in milliseconds
     * @param bool $debug - debug flag when true output additional info
     */
    public function __construct(
        string $address,
        int $port,
        string $login,
        string $password,
        int $timeout = 10000,
        bool $debug = false
    ) {
        $this->transport = new Socket([$address], $port);
        // Activate binary hex-output of server interaction
        $this->transport->debug = $debug;
        $this->transport->setRecvTimeout($timeout);
        $this->smppClient = new SmppClient($this->transport);

        $this->login = $login;
        $this->password = $password;

        $this->from = new Address(self::DEFAULT_SENDER, SMPP::TON_ALPHANUMERIC);

        $peId = getenv('PEID'); // DLT approved PEID (unique Entity ID)
        $templateId = getenv('PEID'); // DLT approved Template ID

        $this->tags = [
            new Tag(0x1470, $peId, strlen((string)$peId)),
            new Tag(0x1471, $templateId, strlen((string)$templateId))
        ];
    }

    /**
     * @param string $sender
     * @param int $ton
     *
     * @return $this
     * @throws Exception
     */
    public function setSender(string $sender, int $ton): SmsBuilder
    {
        return $this->setAddress($sender, 'from', $ton);
    }

    /**
     * @param string $address
     * @param string $type
     * @param int $ton
     * @param int $npi
     *
     * @return $this
     * @throws Exception
     */
    protected function setAddress(
        string $address,
        string $type,
        int $ton = SMPP::TON_UNKNOWN,
        int $npi = SMPP::NPI_UNKNOWN
    ): SmsBuilder {
        // some example of data preparation
        if ($ton === SMPP::TON_INTERNATIONAL) {
            $npi = SMPP::NPI_E164;
        }
        $this->$type = new Address($address, $ton, $npi);

        return $this;
    }

    /**
     * @param string $address
     * @param int $ton
     *
     * @return $this
     * @throws Exception
     */
    public function setRecipient(string $address, int $ton): SmsBuilder
    {
        return $this->setAddress($address, 'to', $ton);
    }

    /**
     * @param string $message
     *
     * @throws Exception
     */
    public function sendMessage(string $message): void
    {
        $this->transport->open();
        $this->smppClient->bindTransceiver($this->login, $this->password);
        // strongly recommend use SMPP::DATA_CODING_UCS2 as default encoding in project to prevent problems with non latin symbols
        $this->smppClient->sendSMS($this->from, $this->to, $message, $this->tags, SMPP::DATA_CODING_UCS2);
        $this->smppClient->close();
    }
}

its not a best practice - its just example, looks like each template should has unique id ($templateId) and be stored in some storage and provided to method sendMessage as argument (for example)

values 0x1470 and 0x1471 would works only with vonage i take it from his docs https://api.support.vonage.com/hc/en-us/articles/204017423-India-SMS-Features-Restrictions other providers may has another values for tags