text: var cp = require('child_process'),
    emitter = require('events').EventEmitter;
  
  module.exports = function(tgPath, keyPath) {
    var instance = new emitter(),
      proc = cp.spawn(tgPath, ['-k', keyPath, '-W']),
      initialized = false;
  
    setTimeout(function() { // TODO: check output from telegram-cli
      initialized = true;
      instance.emit('ready');
    }, 5000);
  
    proc.on('error', function() {
      instance.emit('error', new Error('process killed')); // TODO: add reinit method
    });
  
    proc.stdout.on('data', function(data) {
      instance.emit('message', data);
    });
  
    instance.sendRAW = function(msg) {
      if (initialized) {
        proc.stdin.write(msg + '\n');
      } else {
        instance.emit('error', new Error('unitialized'));
      }
    };
  
    instance.msg = function(to, message) {
      this.sendRAW('msg ' + to + ' ' + message);
    };
  
    instance.close = function() {
      this.sendRAW('safe_quit');
    };
  
    return instance;
  };
  