import Message from 'telegram-api/types/Message';
import request from 'request';

export default function npm(bot) {

  const BASE = 'http://registry.npmjs.org/';

  const ask = new Message().text('What package are you searching for?');
  bot.command('npm', message => {

    const search = (pack, id) => {
      new Promise((resolve, reject) => {
        request(BASE + pack, (err, res, body) => {
          try {
            const result = JSON.parse(body);
            resolve([pack, result.name])
          }
          catch(e) {
            reject();
          }
        });
      }).then((result) => {
        bot.send(new Message().text(result[0] + '\n' + 'https://www.npmjs.com/' + result[1]).to(id))
      }).catch(() => {
        bot.send(new Message().text('No results found :(').to(id));
      });
    }

    let pack = message.text.slice(4).trim();

    if (!pack) {
      bot.send(ask.to(message.chat.id)).then(answer => {
        search(answer.text, message.chat.id);
      });
    } else {
      search(pack, message.chat.id);
    }
  });
}

@@ -286,19 +286,18 @@
 rch = (p
-ac
 k
+g
 , id) =%3E
@@ -476,15 +476,8 @@
 lve(
-%5Bpack, 
 resu
@@ -483,18 +483,18 @@
 ult.name
-%5D
 )
+;
 %0A       
@@ -497,26 +497,16 @@
        %7D
-%0A         
  catch(e
@@ -573,16 +573,14 @@
 hen(
-(
 result
-)
  =%3E 
@@ -585,33 +585,36 @@
 %3E %7B%0A        
-bot.send(
+const msg = 
 new Message(
@@ -618,34 +618,44 @@
 ge()
-.text(result%5B0%5D + '%5Cn' + '
+%0A                    .text(%60$%7Bpkg%7D%5Cn
 http
@@ -676,30 +676,71 @@
 com/
-' + 
+$%7B
 result
-%5B1%5D).to(id))
+%7D%60)%0A                    .to(id);%0A        bot.send(msg);
 %0A   
@@ -842,16 +842,17 @@
 );%0A    %7D
+;
 %0A%0A    le

Refactor npm.js