Announcement

Collapse
No announcement yet.

Copy Attachments from Stream

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Copy Attachments from Stream

    Hello,

    I have a custom entity for WhatsApp in my instance, whenever we receive a whatsapp message a new post gets created in the stream, if the message is a pdf/image that's what get posted.

    This entity has an ActionButton to create a Lead from the WhatsApp conversation if No lead is already present.

    I 'd like to copy all the attachments in the WhatsApp conversation stream to the lead Attachments field in Lead, would that be possibile?
    What's the best way to achieve that? a Hook? Adding the attributes to the handler??
    This is my current code.

    WhatsApp-handler.js
    Code:
    define('custom:WhatsApp-handler', ['action-handler'], function (Dep) {
    
       return Dep.extend({
    
          actionNewLead: function() {
            var attributes = {};
            attributes.phoneNumber = this.view.model.get('from');
            attributes.whatsAppId = this.view.model.get('id');
            attributes.whatsAppName = this.view.model.get('name');
            attributes.source = "WhatsApp";
            var scope = 'Lead'
            var router = this.getRouter();
            router.dispatch(scope, 'create', {
            attributes: attributes,
    
     });
     },
    
            initNewLead: function () {
                this.controlButtonVisibility();
    
                this.view.listenTo(
                    this.view.model,
                    'change:leadId',
                    this.controlButtonVisibility.bind(this)
                );
            },
    
            controlButtonVisibility: function () {
                if (this.view.model.get('leadId') === null) {
                    this.view.showHeaderActionItem('NewLead');
    
                    return;
                }
    
                this.view.hideHeaderActionItem('NewLead');
            },
       });
    });
    ​
    Last edited by Kharg; 09-10-2022, 06:40 PM.

  • #2
    The main thing is that copying just ids is not enough. You need to copy attachment records and set their sourceId = originalAttachmenent.sourseId ?? originalAttachmenent.id. There were a few topic on this here on the forum.

    Comment


    • #3
      Thanks Yuri.
      I kinda solved by posting the attachments to an Attachments field instead of the stream, even though these attachments gets moved to the new parent instead of duplicating.
      Wouldn't it be possible to enable the "foreign" field for Attachement Multiple fields as well? This would solve these kinds of problems easily, what do you think?
      Last edited by Kharg; 09-12-2022, 09:25 AM.

      Comment


      • #4
        > Wouldn't it be possible to enable the "foreign" field for Attachement Multiple fields as well?

        Not possible technically with the current approach.

        Comment


        • #5
          Great idea, but probably should use more of WhatsApp's API for implementation. Ex: https://getmodnow.com/
          Or using Telegram will give better results.
          Sorry for my english
          Last edited by AlanSchultz; 12-12-2022, 04:51 AM. Reason: Example

          Comment


          • #6
            it is possible to copy all the attachments from the WhatsApp conversation stream to the lead attachments field in Lead. You can achieve this by adding a hook to the action button that creates the lead.

            In your WhatsApp-handler.js file, you can add the following code to create a hook for the createLead action button:
            ​// add hook for createLead action button
            $app.on('whatsapp.createLead', async (payload) => {
            const lead = await $db.lead.findOrCreate({ phone: payload.phone });
            const attachments = payload.attachments;

            // Copy attachments to lead attachments field
            lead.attachments = attachments;

            await lead.save();
            });

            Comment

            Working...
            X