0

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: 'juliet@example.com/balcony',
        to: 'romeo@example.net',
        type: 'chat',
      });
      _converse.send(msg);
    },
  });
};

1 Answers1

1

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' });
JC Brand
  • 2,652
  • 18
  • 18