BxDolSmsTwilio.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. require_once BX_DIRECTORY_PATH_PLUGINS . 'twilio/sdk/src/Twilio/autoload.php';
  10. class BxDolSmsTwilio extends BxDolSms
  11. {
  12. protected $_sSid;
  13. protected $_sToken;
  14. protected $_sFromNumber;
  15. protected function __construct($aObject, $oTemplate = null, $sDbClassName = '')
  16. {
  17. parent::__construct($aObject, $oTemplate, $sDbClassName);
  18. $this->_sSid = getParam('sys_sms_twilio_sid');
  19. $this->_sToken = getParam('sys_sms_twilio_token');
  20. $this->_sFromNumber = getParam('sys_sms_twilio_from_number');
  21. }
  22. public function send($sTo, $sMessage, $sFrom = '')
  23. {
  24. try {
  25. $oClient = new Twilio\Rest\Client($this->_sSid, $this->_sToken);
  26. $oClient->messages->create($this->normalizePhone($sTo), [
  27. 'body' => $sMessage,
  28. 'from' => $sFrom != '' ? $this->normalizePhone($sFrom) : $this->normalizePhone($this->_sFromNumber)
  29. ]);
  30. return true;
  31. }
  32. catch (Exception $oException) {
  33. $this->_writeLog($oException->getFile() . ':' . $oException->getLine() . ' ' . $oException->getMessage(), BX_LOG_ERR);
  34. return false;
  35. }
  36. }
  37. }
  38. /** @} */