Purpose of this commit is to support more formatting options in phone tools,
to provide new API methods for phone numbers validatio. It also introduces
a method on sms.api to send SMS in batch. This one uses a new route given
in IAP services that send a batch of SMS instead of sending them one by one.
Concerning phone validation, tools are updated to correctly handle all
format supported by phonenumbers library. Indeed in addition to national
and international formatting phonenumbers library also supports E164
(international without spaces) and RFC3966 (beautification of phone numbers).
Let us support them and ease the use of our small tool methods in various
addons, notably sms.
Cleaning / check tools methods are added in phone validation module. Various
methods allow to sanitize phone numbers according to a context record and/or
some parameters. Those tools will be used in upcoming sms refactoring.
Another purpose of this commit is to add phone_validation in dependencies
of SMS application. Phone_validation module adds tools to parse and format
phone numbers and allow to replace the manual check done currently. Adding
it in dependencies of SMS does not change anything functionally as
* library is still optional. If phonenumbers library is not installed the
features are skipped without crashing;
* no model is modified as it introduces only tools and a mixin. Models are
updated in crm_phone_validation that updates lead and partner models;
Related to task 1922163
Linked to PR #34516
Co-Authored-By: Thibault Delavallee <tde@odoo.com>
Co-Authored-By: Pierre Rousseau <pro@odoo.com>
59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, models
|
|
from odoo.addons.iap.models import iap
|
|
|
|
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.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_format_number' or 'success',
|
|
'credit': integer: number of credits spent to send this SMS,
|
|
}]
|
|
|
|
:raises: normally none
|
|
"""
|
|
params = {
|
|
'messages': messages
|
|
}
|
|
return self._contact_iap('/iap/sms/1/send', params)
|