Adding SMS features to (custom) modules
Intermediate
In Odoo 13 quite some SMS integrations have been added such as sending texts from the contact form, from an event, from the picking and others. Odoo pointed out that SMS is a much better way to engage with your customers than e-mail as it is rather outdated.
In this tutorial you will learn about the possible options to integrate SMS into a (custom) module. You will learn how to add the SMS icon next to a field, how to have an action to send an SMS, how to use SMS templates and how to directly send them.
In this tutorial we will add these three options to the existing quotations. This will allow us to send our customer a custom SMS or sending an SMS template that notifies the customer about a new sale order that he/she can view in the portal.
Before you can start adding SMS features you will need to create a new module and configure the manifest.py correctly. Open up your manifest.py and add 'sms' as a dependency. As we'll add the SMS features on the sale orders we'll also need a dependency to sale:
'depends': ['sms', 'sale']
Without the 'sms' dependency Odoo cannot build up SMS messages and cannot send it as the SMS module contains all the framework logic to handle text messages.
Create a new file named 'sale_order.py' in your 'models' folder and inherit the 'sale.order' model. Next we have to create a new field 'partner_mobile' which is a related to the customer his/her mobile phone. We'll use this field in the form view to set the phone widget and to allow sending an SMS:
from odoo import models, fields, api, _class SaleOrder(models.Model): _inherit = 'sale.order' # Used to add a field in the frontend for showing the 'SMS' feature partner_mobile = fields.Char( related='partner_id.mobile', string='Mobile phone customer' )
That's all the code that you need on the Python side to use the SMS feature on the sale order. Let's now show this 'partner_mobile' field on the order and make sure we can send an SMS from this field.
The first thing that we need is to have our custom field 'partner_mobile' available on the form view. Create a new file named 'sale_order_view.xml' in your 'views' folder and inherit the existing form view to add our custom field in it:
So what does this do? Because we set the "phone" widget on the field Odoo will format our phone numbers in a human-readable format. The options {'enable_sms': True} is a built-in option that is available from the SMS module. By setting this option we will tell Odoo that we want to show the mobile phone icon with the text "SMS" next to it. If you would now install your module you can already click on this button and send an SMS right away:
There are two more options to send an SMS though! The first options is to directly send an SMS with a predefined text from a button/click and the second is from an action.
Let's add a button to our sale order form view with the text "Send customer SMS" as we already have the XML file opened:
The next step is to create a Python function named 'send_sms' in the 'sale.order' model. This function will call the API to send out an SMS. Open up your 'sale_order.py' file from the 'models' folder again and have a look at this code first:
def send_sms(self): self._message_sms_with_template( template_xmlid='tutorial_sms_integration.sms_template_sale_order_reminder', template_fallback=_("Your sale order (%s) is now available on our customer portal at %s.") % (self.name, self.company_id.website + '/my/home'), partner_ids=self.partner_id.ids, put_in_queue=False, )
Does this code make sense to you? Let me explain it in detail to you. The function '_message_sms_with_template' is a builtin function from Odoo. As soon as you have a dependency to the sms module you can use this function. The function expects a few values in order to send out an SMS:
As you've probably already seen we didn't create that template_xmlid yet. The template should be an XML record that is loaded as data in your Odoo module. Create a new file named 'sms_data.xml' in a new folder 'data'. In order to create an SMS template we need to create an XML record that has at least a name, model and a body. The template should be wrapped in a noupdate tag as we don't want it to be reloaded every time. This would cause the loss of changes if somebody modified the template. Your code should look like this:
Tip: don't forget to add 'data/sms_data.xml' to your manifest.py file!
That's it. If you'd install this module now and would click on the button 'Send customer SMS' you'd see that the SMS message is automatically sent and shown in the chatter:
Finally, there is one more way to send SMS messages through Odoo: through an action. By creating an act_window which has a res_model of 'sms.composer' and a context you can get a dialog to add your own text and send an SMS too. Open up the 'sale_order_view.xml' file again and add the following code:
This code will add a new action in the form view under 'Actions' named 'Send SMS to customer'. Thanks to the binding_model Odoo knows that you want to add it to the sale order. The binding_model tells Odoo that we want the SMS dialog and that we want to show a new dialog thanks to 'target'. That's it! If you'd now click on the action 'Send SMS to customer' you'd get the SMS composer:
That's it! You can now show the SMS icon, automatically send text messages and create actions to send SMS messages. You're now set to go and implement SMS features in any way you'd like.
Since Odoo V13 the SMS integration got even stronger and there are a lot of options now. Thanks to the action, widget and the function we can send SMS messages in no time at a lower price than creating your own connector. As the flexibility in Odoo is pretty big this should allow you to implement SMS in any way you'd like. It is becoming a more popular way of communicating with your customers so be sure to benefit from this.