问题
I am making a module on Odoo to send sms from a mailing list. I have run this code but I have an error.
contact_list_ids = fields.Many2many('mail.mass_mailing.list', 'phone_number', string='Liste de diffusion')
messages_sms = fields.Text(string="message", required=True)
# send SMS with GET method
@api.multi
def send_sms(self):
for list in self.contact_list_ids:
for contact in list:
final_url = (
URL +
'&to=' +contact +
'&sms=' + self.messages_sms
)
r = requests.get(final_url)
if not r:
return ERROR_API
return r.text
The error:
File "c:\program files (x86)\odoo 12.0\server\addons\KeoMarketing\models\messages_sms.py", line 36, in send_sms '&sms=' + self.messages_sms TypeError: must be str, not mail.mass_mailing.list
回答1:
You got a type error because you can't concatenate str with a mail.mass_mailing.list
record.
'&to=' + contact
Try to pass the phone number instead (or any field used to store the phone number):
'&to=' + contact.phone_number
You can check the following Twilio
example:
from twilio.rest import Client
class CustomClass(models.Model):
contact_list_ids = fields.Many2many('mail.mass_mailing.list', 'phone_number', string='Liste de diffusion')
messages_sms = fields.Text(string="message", required=True)
@api.multi
def send_sms(self):
self.ensure_one()
account_sid = ''
auth_token = ''
client = Client(account_sid, auth_token)
for list in self.contact_list_ids:
for contact in list.contact_ids:
client.messages \
.create(
body=self.messages_sms,
from_=self.env.user.partner_id.mobile,
status_callback='http://postb.in/1234abcd',
to=contact.phone_number
)
class MailMassMailingContact(models.Model):
_inherit = 'mail.mass_mailing.contact'
phone_number = fields.Char()
The phone number used to send SMS is stored in the related partner of the current user
I inherited
mail.mass_mailing.contact
to add a phone number for each contact
来源:https://stackoverflow.com/questions/61954847/typeerror-must-be-str-not-mail-mass-mailing-list