Botkit: close all convos before action

Created on 28 Apr 2017  路  9Comments  路  Source: howdyai/botkit

Hi!

I got a problem when I send a subscription broadcast. I create this broadcast with:

schedule.scheduleJob('0 7 * * *', function(){
  console.log('*****Env铆o de suscripcion*****');

  var carruselPortada = [];

  feedRefresh();

  feedPortada.forEach(function(item, index){
    if(index<4)
      carruselPortada.push({
        'title': item.title,
        'item_url': item.link,
        'image_url': item.image.url,
        'subtitle': limpiarHTML(item.summary),
        'buttons':[
          {
            'type':'web_url',
            'url': item.link,
            'title':'Ver noticia'
          },
          {
            'type': 'element_share'
          }
        ]
      }
    )
  });

  controller.storage.users.all(function(err, data){
    data.forEach(function(user){
      if(user.subscription){
        bot.startConversation({
          user: user.id,
          channel: user.id,
          text: 'dummy'
        }, function(err, convo) {
          convo.say('Aqu铆 tienes las noticias de hoy');
          convo.ask({
          'attachment': {
            'type':'template',
            'payload':{
              'template_type':'generic',
              'elements': carruselPortada
          }
        },
        'quick_replies':[
        {
          "content_type": "text",
          "title": "M谩s noticias",
          "payload": "NOTICIAS_PAYLOAD",
        },
        ]
          },[
              {
                pattern: /(m谩s|noticias)/gi,
                callback: function(response, convo) {
                  elegirtema(convo);
                  convo.next();
                }
              },
              {
                default: true,
                callback: function(response, convo) {
                  smarthandler(response,convo);
                  convo.next();
                }
              }
          ]);
        });
      };
    });
  });
});

Problem is when I made this other convo can exist so when user use subs quickreply the other convo is triggered.

How can I close all convos before this?

help wanted

Most helpful comment

Yes, i juste share with you how to get conversations from the tasks object ;)

You need to iterate over them and stop the concerned ones :

for (let task of tasks) {
            for (let convo of task.convos) {
                if (convo.isActive() && convo.source_message.user == userId) {
                    convo.stop();
                }
            }
        }

let me know if it works for you !!

All 9 comments

I am not sure if I understand your use case, but to close all conversations you can call convo.stop() of each conversation in each task.

let tasks = controller.task() // Get a task array from the controller
let convos = tasks[0].convos() // Get an array of conversation from a task object
convos[0].stop() // Stop a conversation

I get this :(

info: ERROR IN RECEIVE MIDDLEWARE: TypeError: controller.task is not a function

Try .tasks[] instead of .task()

I didn't try yet but I would like to do with knowledge, What is task? There is a task for each user? @ouadie-lahdioui

Tasks can contain multiple conversations, and trigger a collective end event when all child conversations have ended.

They are useful if you have for example some operatiosn that involves talking to several people all at once (the task can contain them all).

@benbrown planning on removing it soon

@ouadie-lahdioui problem is that I have to close convos of a specific user, not all.

Yes, i juste share with you how to get conversations from the tasks object ;)

You need to iterate over them and stop the concerned ones :

for (let task of tasks) {
            for (let convo of task.convos) {
                if (convo.isActive() && convo.source_message.user == userId) {
                    convo.stop();
                }
            }
        }

let me know if it works for you !!

@ouadie-lahdioui that's clever . thanks!

use controller.tasks to iterate tasks. #19/01/2018

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JonnyBoy333 picture JonnyBoy333  路  3Comments

HannanShaik picture HannanShaik  路  3Comments

fieldcorbett picture fieldcorbett  路  4Comments

matueranet picture matueranet  路  4Comments

simpixelated picture simpixelated  路  3Comments