TypeError: must be str, not mail.mass_mailing.list

杀马特。学长 韩版系。学妹 提交于 2020-05-30 08:00:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!