summaryrefslogtreecommitdiff
path: root/addons/sms/models/sms_api.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/sms/models/sms_api.py
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/sms/models/sms_api.py')
-rw-r--r--addons/sms/models/sms_api.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/addons/sms/models/sms_api.py b/addons/sms/models/sms_api.py
new file mode 100644
index 00000000..b1fa69da
--- /dev/null
+++ b/addons/sms/models/sms_api.py
@@ -0,0 +1,58 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, models
+from odoo.addons.iap.tools import iap_tools
+
+DEFAULT_ENDPOINT = 'https://iap-sms.odoo.com'
+
+
+class SmsApi(models.AbstractModel):
+ _name = 'sms.api'
+ _description = 'SMS API'
+
+ @api.model
+ def _contact_iap(self, local_endpoint, params):
+ account = self.env['iap.account'].get('sms')
+ params['account_token'] = account.account_token
+ endpoint = self.env['ir.config_parameter'].sudo().get_param('sms.endpoint', DEFAULT_ENDPOINT)
+ # TODO PRO, the default timeout is 15, do we have to increase it ?
+ return iap_tools.iap_jsonrpc(endpoint + local_endpoint, params=params)
+
+ @api.model
+ def _send_sms(self, numbers, message):
+ """ Send a single message to several numbers
+
+ :param numbers: list of E164 formatted phone numbers
+ :param message: content to send
+
+ :raises ? TDE FIXME
+ """
+ params = {
+ 'numbers': numbers,
+ 'message': message,
+ }
+ return self._contact_iap('/iap/message_send', params)
+
+ @api.model
+ def _send_sms_batch(self, messages):
+ """ Send SMS using IAP in batch mode
+
+ :param messages: list of SMS to send, structured as dict [{
+ 'res_id': integer: ID of sms.sms,
+ 'number': string: E164 formatted phone number,
+ 'content': string: content to send
+ }]
+
+ :return: return of /iap/sms/1/send controller which is a list of dict [{
+ 'res_id': integer: ID of sms.sms,
+ 'state': string: 'insufficient_credit' or 'wrong_number_format' or 'success',
+ 'credit': integer: number of credits spent to send this SMS,
+ }]
+
+ :raises: normally none
+ """
+ params = {
+ 'messages': messages
+ }
+ return self._contact_iap('/iap/sms/2/send', params)