broidHQ / integrations

Connect your App to Multiple Messaging Channels with the W3C Open standard.
https://www.broid.ai
GNU Affero General Public License v3.0
745 stars 83 forks source link

fix(telegram): add missing listen call when calling connect #129

Closed m90 closed 7 years ago

m90 commented 7 years ago

As just discussed in Gitter, calling connect on a telegram client will currently not start the express server as noone ever calls its listen method, resulting in an telegram only integration exiting cleanly directly after startup.

This is an example of an application that will instantly exit with broid/telegram@2.0.0 installed:

const R = require('ramda');
const BroidTelegram = require('@broid/telegram');

const telegram = new BroidTelegram({
    token: '<TOKEN>'
    webhookURL: 'https://<APP>.herokuapp.com',
    http: {
        host: '127.0.0.1',
        port: process.env.PORT
    }
});

telegram.connect()
    .subscribe({
        next: data => console.log(JSON.stringify(data, null, 2)),
        error: err => console.error(`Something went wrong: ${err.message}`),
        complete: (...args) => console.log('Done!', args)
    });

telegram.listen()
    .subscribe({        
        next: data => {
            if (data.type === 'Create') {
                const to = R.path(['target', 'id'], data);
                const to_type = R.path(['target', 'type'], data);
                const service_id = R.path(['generator', 'id'], data);
                const service_name = R.path(['generator', 'name'], data);

                const message = {
                    '@context': 'https://www.w3.org/ns/activitystreams',
                    'type': 'Create',
                    'generator': {
                        'id': service_id,
                        'type': 'Service',
                        'name': service_name
                    },
                    'object': {
                        'type': 'Note',
                        'content': 'Hello, how are you?'
                    },
                    'to': {
                        'type': to_type,
                        'id': to
                    }
                };

                telegram
                    .send(message)
                    .then(console.log)
                    .catch(console.error);
            }   
        },
        complete: () => console.log('Listen complete'),
        error: err => console.error(`Something went wrong: ${err.message}`)
    });

The output is the following:

> node app.js

{
  "type": "connected",
  "serviceID": "3ba3c49c-0127-4c77-a587-5a72c59107eb"
}
Done! [ undefined ]
killix commented 7 years ago

Can you make the fix directly on the typescript file ? JS files are build each time we deploy.