momocow / node-cq-websocket

A Node SDK for developing QQ chatbots based on WebSocket, which is depending on CoolQ and CQHTTP API plugin.

Home Page:https://cq-websocket.js.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can you guide me how to ues it?

ChrisWen960216 opened this issue · comments

I download the cpk for CoolQ HTTP and install the package for node-cq-websocket,how can I start?

I'll provide some simple steps to demo the usage.

To learn more, it is recommended to read the document of both node-cq-websocket and CQHTTP API. Besides, you can join the QQ group of CQHTTP API to ask for helps.


  1. Deploy your CoolQ instance with CQHTTP API plugin configured. (Read more to learn how to configure CQHTTP API). Make sure you have WebSocket enabled when configuring CQHTTP API.
# config.ini
use_ws = true # it is false by default

# for convenience, you can leave the rest of configs by default
  1. In your nodejs project, create a CQWebSocket instance with default configs (or read this to customize your config).
const CQWebSocket = require('cq-websocket')

// default configs
let bot = new CQWebSocket()

// listen on all types of messages and print them to the console
bot.on('message', (e, context) => {
    console.log(context.raw_message)

    return 'haha'; // quick response
})

// when the connection is ready
bot.on('ready', function () {
  /**
   * bot is also a callable instance, call it when you want to call the APIs provided by CQHTTP
   * to know what methods and params you can use, read the following link.
   * @see https://cqhttp.cc/docs/4.4/#/API?id=api-%E5%88%97%E8%A1%A8
   */
  bot('send_private_msg', {
    user_id: 'xxxxxxxx',
    message: 'Hello'
  });
})
  1. Launch your nodejs bot on the same host as CoolQ instance (because the default host of CQWebSocket is 127.0.0.1) and say something to the bot and it will response you with "haha".

Caveat

  • Ensure use_ws is enabled (yes, true or 1) in the config file of CQHTTP API.
  • If you want to customize your host and port, make sure that the host and port provided to CQWebSocket constructor always matches with those configured in the config file of CQHTTP API, which are ws_host and ws_port.

Thanks for your guide,I will try.

I'll close this issue since it has been idle for a week.
Feel free to reopen it if you need further support.