2

I'm looking into ways to open a chat window with a user via the API.

I have set up (and registered in the whitelist) a plugin, which triggers.

    converse.plugins.add('startChat', {
        initialize: function () {
            this._converse.on('connected', function () {
                console.log("wibble");
                this._converse.api.chats.open('user@chat.domain.com');
            });
          }
      });

This is firing, but no chat is initiated. I get the following errors in console:

wibble

converse.js:48049
FATAL: TypeError: Cannot read property 'get' of undefined
    at child.createChatBox (/converse.js:50015:56)
    at child.getChatBox (/converse.js:50050:40)
    at Object.open (/converse.js:50153:84)
    at Object.<anonymous> (Page.aspx:1264:41)
    at triggerEvents (/converse.js:46015:57)
    at triggerApi (/converse.js:46003:19)
    at eventsApi (/converse.js:45802:16)
    at Object.Events.trigger (/converse.js:45993:5)
    at Object._converse.emit (/converse.js:48071:27)
    at Object.onConnected (/converse.js:48671:27)

converse.js:48041 
ERROR: User connection callback caused an exception: TypeError: Cannot read property 'get' of undefined
sbozzie
  • 717
  • 2
  • 12
  • 33

2 Answers2

1

Firstly, Converse.js doesn't allow opening chats with users who aren't in your roster (unless you have set allow_non_roster_messaging to true). So make sure the user you're trying to chat with is in your roster.

Secondly, right after the connection event has been fired is still too early to try and open chats.

So you have to first wait until the roster has been populated. This is the rosterContactsFetched event/promise.

Besides that, it's a good idea to also first wait until already opened chat boxes (which are cached in sessionStorage) have also been fetched from the cache. This is the chatBoxesFetched event/promise.

These two events fire only once per session, so they're also available as promises.

To first wait for these two promises to resolve, you're code would therefore look like this:

converse.plugins.add('startChat', {
  initialize: function() {
    var _converse = this._converse;
    Promise.all([
        _converse.api.waitUntil('rosterContactsFetched'),
        _converse.api.waitUntil('chatBoxesFetched')
    ]).then(function() {
      // Note, test@conversejs.org must be in your contacts roster!
      _converse.api.chats.open('test@conversejs.org');
    });
  } 
});

Here's a JSFiddle which runs the above code: https://jsfiddle.net/jcbrand/5juvrL3c/

JC Brand
  • 2,652
  • 18
  • 18
0

And for future travellers: I solved starting a converse chat from outside converse like so:

<li id="start-chat" data-id="idOfTheUser" data-name="nameOfTheUser"> Message</li>

Then a plugin like so:

    converse.plugins.add('startChat', {
        initialize: function () {
            var _converse = this._converse;

            $('#start-chat').on('click', function () {

                var jid = $('#start-chat').attr("data-id") +'@chat.domain.com';
                var userFullName = $('#start-chat').attr("data-name");

                _converse.api.contacts.add(jid, userFullName);
                _converse.api.chats.open(jid, true);
            });
          }
      });
sbozzie
  • 717
  • 2
  • 12
  • 33