Twillio Programmble聊天消息侦听器返回重复的消息

时间:2020-07-01 12:04:39

标签: javascript node.js vue.js twilio twilio-programmable-chat

我在twillio自动驾驶仪上设计了一个聊天组件。打开聊天弹出窗口时,令牌生成,客户端创建以及加入频道的操作均正确进行。

/*JS file*/
async createOrJoinGeneralChannel(NULL, { identity }) {
    globaldata.client
      .getChannelByUniqueName(identity)
      .then(uniqueChannel => {
        globaldata.channel = uniqueChannel;
        uniqueChannel.join().then(createdChannel => {
          // commit(M.SET_CHANNEL, uniqueChannel);
          console.log('Successfully setted the channel', createdChannel);
        });
      })
      .catch(() => {
        globaldata.client
          .createChannel({
            uniqueName: identity,
            friendlyName: identity,
            isPrivate: true,
          })
          .then(uniqueChannel => {
            globaldata.channel = uniqueChannel;
            uniqueChannel.join().then(createdChannel => {
              // commit(M.SET_CHANNEL, uniqueChannel);
              console.log('Successfully setted the channel', createdChannel);
            });
          })
          .catch(e => {
            console.log('Channel could not be created: ', e);
          });
      });
  },

async createClient({ dispatch }, data) {
    twillioChat.Client.create(data.token)
      .then(client => {
        console.log('Created chat client');
        client.getSubscribedChannels().then(() => {
          // commit(M.SET_CLIENT, client);
          globaldata.client = client;
          dispatch('createOrJoinGeneralChannel', { identity: data.identity });
        });
        client.on('tokenAboutToExpire', () => {
          dispatch('tokenGenerator');
        });
        client.on('tokenExpired', () => {
          dispatch('tokenGenerator');
        });
      })
      .catch(error => {
        console.error(error);
      });
  },

我正在使用以下功能来发送消息并侦听来自bot的新消息。侦听器返回重复的消息。

function sendMessage(commit, msgData) {   let response = {};   const chatMsg = msgData.data.text;   if (chatMsg && globaldata.channel) {
    globaldata.channel.sendMessage(chatMsg);
    globaldata.channel.on('messageAdded', newMessage => {
      console.log('listener', newMessage);
 
    });   }   console.log('Empty Channel');   return ''; }



async sendMessage({ commit }, msgData) {
    return sendMessage(commit, msgData);
  },

有人可以帮助我解决此问题吗?预先感谢

0 个答案:

没有答案