项目作者: JSquadders

项目描述 :
Browser-automated chatbot.
高级语言: JavaScript
项目地址: git://github.com/JSquadders/ChatBot.git
创建时间: 2019-10-09T16:28:34Z
项目社区:https://github.com/JSquadders/ChatBot

开源协议:

下载


JSquadBot

Automation-based chatbot that is able to read and answer messages from a browser-based chat such as WebWhatsApp.

It’s not integrated directly with the chat API. Instead, it emulates the user in the browser and lets the webpage handle the requests. It was made like that not because it was the best approach, but for learning purposes.

Only WhatsApp is supported out of the box, but, since it was developed in MVVM, it should be easy to make it work anywhere else. Basically, you only need to implement these methods, which are responsible for handling the DOM.

Another advantage from the MVVM approach is that you can use only the functionality that matters to you. For instance, you can take only the View layer and use it for a purpose other than a chatbot.

Usage

  1. Install an extension to skip CORS in your browser, like CORS Everywhere. You can also figure out other ways to skip CORS.

  2. Disable CSP. In Firefox, go to about:config and disable the option security.csp.enable.

    Disable CSP

  3. Open a new tab in your browser and access the clever bot website so that your browser can load the required cookies.

  4. Inject all the code from jsquadbot.min.mjs into the webpage. There are several ways to do that. The most straightforward one is to just copy and paste it into the browser console. Another way is to use a browser extension that allows JS code to be injected automatically.

The injected code does nothing until it’s actually called from console. See the examples below.

Examples

Step by step

  1. let cv = new ChatViewWhatsApp('Eliza');
  2. // This class has methods that deal with the DOM.
  3. // "Eliza" is the name or part of the name of the contact or group you want to attach the bot to.
  4. // The ID should match only one chat.
  5. let cc = new ChatController(cv);
  6. // This class exposes higher level methods to manage the View.
  7. await cc.addBot('HAL')
  8. // Add a bot named "HAL" to the conversation with "Eliza".
  9. chatControllerMap.set(cc);
  10. // chatControllerMap is a global object of the class ChatControllerMap, which is derived from the native Map.
  11. // This is the top level class that manages all chats and bots.
  12. // There must be only 1 instance of ChatControllerMap, otherwise there will be concurrency problems.
  13. await chatControllerMap.listen();
  14. // Bot will start reading and answering messages that contain its name.

Short version

  1. chatControllerMap.set(new ChatController(new ChatViewWhatsApp('Eliza')));
  2. await chatControllerMap.get('Eliza').addBot('HAL');
  3. await chatControllerMap.listen();

Multiple bots and chats

  1. let cc1 = new ChatController(new ChatViewWhatsApp('Eliza'));
  2. await cc1.addBot('HAL');
  3. await cc1.addBot('Siri'); // second bot
  4. let cc2 = new ChatController(new ChatViewWhatsApp('Alice')); // another chat
  5. await cc2.addBot('Cortana'); // another bot
  6. chatControllerMap.set(cc1, cc2); // add both chats at once
  7. await chatControllerMap.listen(); // all 3 bots starts listening
  8. let cc3 = new ChatController(new ChatViewWhatsApp('Bob')); // yet another chat
  9. await cc3.addBot('Terminator');
  10. chatControllerMap.set(cc3); // add chat. New bot starts listening

Using the lib for other purposes

  1. let view = new ChatViewWhatsApp('Bob');
  2. await view.postMessage('Hello!'); // sends message to Bob