Is there any way to send a message automatically in converse.js

117 Views Asked by At

Is there any way to send a message automatically, I found a sendMessage trigger but it doesn't work, maybe I am doing wrong something

export const msgUser = (viewerChannelCredentials: any) => {
  const {username, channel} = viewerChannelCredentials;
  window.converse.plugins.add('msg-user', {
    dependencies: [],
    initialize: async function () {
      const _converse = this._converse;

      return _converse.api.trigger('sendMessage', {
        chatbox: _converse.ChatBox | _converse.ChatRoom,
        message: 'abc',
      });
    },
  });
};

Another solution found that also doesn't work, it throws an error which Cannot read properties of undefined (reading '$msg')

export const msgUser = (viewerChannelCredentials: any) => {
  const {username, channel} = viewerChannelCredentials;
  window.converse.plugins.add('msg-user', {
    dependencies: [],
    initialize: async function () {
      const _converse = this._converse;

      var msg = _converse.env.$msg({
        from: '[email protected]/balcony',
        to: '[email protected]',
        type: 'chat',
      });
      _converse.send(msg);
    },
  });
};
1

There are 1 best solutions below

0
JC Brand On BEST ANSWER

Calling api.trigger('sendMessage' only fires an event with the name "sendMessage", it doesn't actually send a message.

To send a message, you can call the sendMessage method on a ChatBox.

For example:

const chat = await api.chats.get(jid)
chat.sendMessage({ body: 'Hello world' });