grousso

TELEGRAM SMS PROVIDER FOR VTIGER

Jan 21st, 2020
420
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.79 KB | None | 0 0
  1. <?php
  2. /*********************************************************************************
  3.  ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
  4.  * ("License"); You may not use this file except in compliance with the License
  5.  * The Original Code is: vtiger CRM Open Source
  6.  * The Initial Developer of the Original Code is vtiger.
  7.  * Portions created by vtiger are Copyright (C) vtiger.
  8.  * All Rights Reserved.
  9.  
  10.  ********************************************************************************/
  11.  
  12. class SMSNotifier_telegram_Provider implements SMSNotifier_ISMSProvider_Model {
  13.  
  14.     private $_username;
  15.     private $_password;
  16.     private $_parameters = array();
  17.  
  18.     const SERVICE_URI = 'https://api.telegram.org/';
  19.  
  20.     private static $REQUIRED_PARAMETERS = array('from');
  21.  
  22.     function __construct() {
  23.        
  24.     }
  25.  
  26.     public function getName() {
  27.         return 'telegram';
  28.     }
  29.  
  30.     public function setAuthParameters($username, $password) {
  31.         $this->_username = $username;
  32.         $this->_password = $password;
  33.     }
  34.  
  35.     public function setParameter($key, $value) {
  36.         $this->_parameters[$key] = $value;
  37.     }
  38.  
  39.     public function getParameter($key, $defvalue = false) {
  40.         if (isset($this->_parameters[$key])) {
  41.             return $this->_parameters[$key];
  42.         }
  43.         return $defvalue;
  44.     }
  45.  
  46.     public function getRequiredParams() {
  47.         return self::$REQUIRED_PARAMETERS;
  48.     }
  49.  
  50.     public function getServiceURL($type = false) {
  51.         if ($type) {
  52.             switch (strtoupper($type)) {
  53.                 case self::SERVICE_AUTH : return self::SERVICE_URI . 'bot"BOT TOKEN"/sendMessage';
  54.                 case self::SERVICE_SEND : return self::SERVICE_URI . 'bot"BOT TOKEN"/sendMessage';
  55.                 case self::SERVICE_QUERY : return self::SERVICE_URI . '/http/query.php';
  56.             }
  57.         }
  58.         return false;
  59.     }
  60.  
  61.     protected function prepareParameters() {
  62.         $params = array('username' => $this->_username, 'password' => $this->_password);
  63.         foreach (self::$REQUIRED_PARAMETERS as $key) {
  64.             $params[$key] = $this->getParameter($key);
  65.         }
  66.         return $params;
  67.     }
  68.  
  69.     public function send($message, $tonumbers) {
  70.         if (!is_array($tonumbers)) {
  71.             $tonumbers = array($tonumbers);
  72.         }
  73.         foreach ($tonumbers as $i => $tonumber) {
  74.             $tonumbers[$i] = str_replace(array('(', ')', ' ', '+', '-'), '', $tonumber);
  75.         }
  76.  
  77.         $params = $this->prepareParameters();
  78.         $params['text'] = $message;
  79.         $params['chat_id'] = implode(',', $tonumbers);
  80.  
  81.         $serviceURL = $this->getServiceURL(self::SERVICE_SEND);
  82.         $httpClient = new Vtiger_Net_Client($serviceURL);
  83.         $messageId = $httpClient->doGet($params);
  84.         $queryResult = $this->query($messageId);
  85.         $result = array(
  86.             'id' => $messageId,
  87.             'status' => $queryResult['status'],
  88.             'error' => false
  89.         );
  90.         if ($queryResult['status'] == self::MSG_STATUS_FAILED) {
  91.             $result['error'] = true;
  92.         }
  93.         $results = array();
  94.         foreach ($tonumbers as $i => $tonumber) {
  95.             $results[$i] = $result;
  96.             $results[$i]['chat_id'] = $tonumber;
  97.         }
  98.  
  99.         return $results;
  100.     }
  101.  
  102.     public function query($messageid) {
  103.         $params = array('username' => $this->_username, 'password' => $this->_password, 'msgid' => $messageid);
  104.         $serviceURL = $this->getServiceURL(self::SERVICE_QUERY);
  105.         $httpClient = new Vtiger_Net_Client($serviceURL);
  106.         $response = $httpClient->doGet($params);
  107.         $result = array();
  108.         if (stripos($response, 'Delivered') !== false) {
  109.             $result['status'] = self::MSG_STATUS_DISPATCHED;
  110.             $result['needlookup'] = 0;
  111.             $result['statusmessage'] = 'Message delivered';
  112.         } else if (stripos($response, 'Submitted') !== false) {
  113.             $result['status'] = self::MSG_STATUS_PROCESSING;
  114.             $result['needlookup'] = 1;
  115.             $result['statusmessage'] = 'Message submitted for processing';
  116.         } else {
  117.             $result['status'] = self::MSG_STATUS_FAILED;
  118.             $result['needlookup'] = 0;
  119.             $result['statusmessage'] = 'Message delivery failed.';
  120.         }
  121.  
  122.         return $result;
  123.     }
  124.  
  125. }
  126.  
  127. ?>
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment