Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
4 Odpowiedzi
36724 Widoki

I am trying to programmatically post a message (not email). But I don't know how. The message for example can show up in the discussion app , #generals channel. I searched the internet but all I get it is how to send email messages to users using the mail.thread object.

in other words how to accomplish the following using python code

<record model="mail.message" id="module_install_notification">
<field name="model">mail.channel</field>
<field name="res_id" ref="mail.channel_all_employees"/>
<field name="message_type">notification</field>
<field name="subtype_id" ref="mail.mt_comment"/>
<field name="subject">Project Management application installed!</field>
<field name="body"><![CDATA[<p>Manage multi-level projects and tasks. You can delegate tasks, track task work, and review your planning.</p>
<p>You can manage todo lists on tasks by installing the <i>Todo Lists</i> application, supporting the Getting Things Done (GTD) methodology.</p>
<p>You can also manage issues/bugs in projects by installing the "Issue Tracker" application.</p>]]></field>
</record>

The above xml code posts the "body" to the #general channel of the Discuss app.

I am using odoo v9.

Awatar
Odrzuć
Najlepsza odpowiedź

Hello,

First of all, create a channel for a group of users. For example, I want to create for accountant group like this:


Accountant

Message Notifications.

then create a record in mail.message model like this:

def action_send_notification(self):
    self.env['mail.message'].create({
'email_from': self.env.user.partner_id.email, # add the sender email
'author_id': self.env.user.partner_id.id, # add the creator id
'model': 'mail.channel', # model should be mail.channel
'type': 'comment',
'subtype_id': self.env.ref('mail.mt_comment').id, Leave this as it is
'body': "Body of the message", # here add the message body
'channel_ids': [(4, self.env.ref('module_name.channel_accountant_group').id)], # This is the channel where you want to send the message and all the users of this channel will receive message
'res_id': self.env.ref('modulename.channel_accountant_group').id, # here add the channel you created.
})

This code will help you to understand and send messages (not email) programmatically).

Thank You.

Awatar
Odrzuć
Autor Najlepsza odpowiedź

For now I have figured out a way to send messages to followers of a record when a field's value is changed.

Suppose I have a field x.

x=fields.Integer('a var', track_visibility='onchange')
    the track_visibility attribute tells odoo to log any change of x , either from a form view or by a function. The log is posted as an internal note. For this to happen your class must inhert mail.thread and use appropriate widget in the definition of the form view. For that check out  https://www.odoo.com/documentation/10.0/reference/mixins.html


    The internal log note taken by the track_visibility option can be sent as a message to users following the record. On the left of the open chatter you can see that there is a button to add followers for the record. There is also a button which says 'following'. Clicking it gives a list of options with check marks for each. Followers of the record will get messages corresponding to the events represented by each choice in the list. We will first have to add a choice to this list. A choice that corresponds to change of value of x. It is called message sub type.
    In the xml file where you define your form,list...views put the following.
    <record id="mt_x_value_changed" model="mail.message.subtype">
    <field name="name">The value of x changed</field>
    <field name="res_model">modelname</field>
    <field name="default" eval="True"/>
    <field name="description">The value of x changed</field>
    </record>
     Then in your module python file override the _track_subtype(init_values) method. init_values contains a dictionary of changed fields and their previous values. self.x is the current x value. If the option 'The value of x changed' is checked, when x is changed the followers of the record will get a message saying the 'The value of x changed'. In this case it  only when x is < 5 the message is sent.



    def _track_subtype(self,init_values):
    self.ensure_one()
    if (('x' in init_values) and (self.x <= 5)):

    return 'module_name.mt_x_value_changed'
    return super(class_name,self)._track_subtype(init_values)
    Awatar
    Odrzuć
    Najlepsza odpowiedź

    Hi raiye hailu,

    To post a message that can seen by specific recipients,you can simply write below python code into your model:

     partner_ids = self.env['res.partner'].search(search_domain).ids

     # search domain to filter specific partners

     self.message_post(subject=subject,body=body,partner_ids=[(4, [partner_ids])])

     Hope this will helpful to you!

    Thanks!

    Awatar
    Odrzuć
    Autor

    Thank you Jainesh for the answer. But it did not work. Even if it did, I think your code sends email message to partners using the email address input when the partners were created. I don't want to send email messages. I want to send a message to a user of odoo(or post a message to a channel).

    I expect the solution to contain references to res.users not res.partner(or contain a reference a channel id).

    Najlepsza odpowiedź

    Dude!!! you are commenting python code with //

    Awatar
    Odrzuć
    Powiązane posty Odpowiedzi Widoki Czynność
    4
    sty 24
    13345
    0
    gru 15
    4831
    2
    gru 24
    3000
    0
    lip 24
    1174
    1
    wrz 21
    6518