Editor’s note: Long time no see, today’s article comes from chuangyu front end reader contribution. Chuangyu front-end accepts readers’ original technology sharing published or reprinted by authorization. We will fully respect all rights and interests of readers, and express our appreciation to the authors of selected articles by giving them optional technical books

background

In my opinion, no matter how awesome the technology is, it will always be an armchair soldier, adhering to the school motto “Apply what you learn”. A word “dry” is done 💪.

In 2019, how to put front-end knowledge to use 🤔️? Some time ago, the big guys in the QQ group suddenly played CoolQ robot, with the robot online chat, online song, online search map and even… (Everything you can think of 😏)

Suddenly the brain flash a light ⚡️, god cow beep ah, I also want to play! As a front end who wakes up every morning with a new wheel, how about a tech post!

What is CoolQ

To be honest, I had no idea what CoolQ was after looking at the CoolQ website for a long time. Don’t you even tell me what you’re doing? Bad review.

(Editor’s note: There are historical reasons for this…)

The following introduction is from Baidu Baike:

Cool Q is a powerful robot software based on Smart protocol. It can automatically review others’ application for group entry, automatically kick people, automatically manage groups and other automatic operations by installing plug-ins. It can also realize automatic group chat and automatic chat, which plays an important role in activating group atmosphere and saving your precious time.

Cool beep, next I just use CoolQ message push function 😳.

Sources of technical articles

What would you do if you had to do it? You might think of a reptile. You can climb pages, you can climb interfaces… I’m going to use RSS here.

Some of our younger friends may not have heard of RSS, tears of the age… You can read about it here: www.runoob.com/rss/rss-int…

However, not all sites offer RSS, such as Nuggets. Here is Amway RSSHub, which gets page content for us and generates RSS feeds for us lazy people. If you don’t find what you’re looking for, you’ll have to rip the code yourself

In field

1. Install CoolQ

CoolQ is available for Windows only, so if you want to install CoolQ on Linux or macOS, you are advised to install the image using Docker. (9102 years, if you still can’t Docker you are out, some knowledge is not deep can, but a wide point is no problem 🙃)

We usually install the normal version.

If the default features don’t meet your needs and you want to develop some fun new features yourself, you can install the development version.

Let’s take a look at how these two versions are installed.

Normal version Installation

1.1. Obtaining a Mirror

docker pull coolq/wine-coolq
Copy the code

1.2. Create a folder for storing CoolQ persistent data

#Any path can be used
mkdir /root/coolq-data
Copy the code

1.3. Run an image

docker run --name=coolq --rm -p 9000:9000 -v /root/coolq-data:/home/user/coolq coolq/wine-coolq
Copy the code
  • --nameCreate a container
  • --rmThis parameter is used to delete the container after it exits. By default, exited containers are not deleted immediately, unless manually, in order to remove barriersdocker rm
  • -p < host port >:< container port > -pIs used to map host ports to container ports, in other words, to expose the container’s corresponding port services to external access
  • -vSpecifies to mount a localhost directory to the container

1.4. Start CoolQ

Open a browser and type localhost:9000

Click Connect to enter the default password MAX8char

Enter your QQ id and password to log in to CoolQ

Development version Installation

1.1. Obtaining a Mirror

docker pull richardchien/cqhttp:latest
Copy the code

1.2. Create a folder for storing CoolQ persistent data

1.3. Run an image

docker run -ti --rm --name cqhttp -p 9000:9000 -p 5700:5700 -v /root/coolq-data:/home/user/coolq richardchien/cqhttp
Copy the code

1.4. Start CoolQ

2. Install the mongo

Slightly (I believe this must not be the small genius of you 😉)

  • The official documentation

  • Rookie document

3. Lu code

Climb the article

async function crawl(url) {
 try {
   const feed = await parser.parseURL(url);
   const items = feed.items.map(({ title, link, guid = link }) = > {
     title = title.trim();
     link = link.trim();
     guid = guid.trim();

     console.log(title, link);
     return { title, link, guid };
   });

   return items;
 } catch (err) {
   console.log(err); }}Copy the code

Database insert data

async function insert(db, { title, link, guid }) {
 const collection = db.collection(collectionName);
 // Insert some documents
 try {
   await collection.updateOne(
     {
       guid
     },
     {
       $set: { title, link, guid },
       $setOnInsert: { status: 0}}, {upsert: true}); }catch (err) {
   console.log(err); }}Copy the code

Database lookup data

Status 0: not pushed 1: pushed

async function find(db) {
 const collection = db.collection(collectionName);
 // Find some documents
 try {
   return await collection
     .find({
       status: 0
     })
     .toArray();
 } catch (err) {
   console.log(err); }}Copy the code

Push message

Group_id group no.

const request = require('superagent');

async function send(message) {
 return await request
   .post('http://0.0.0.0:5700/send_group_msg')
   .send({ group_id: XXX, message })
   .set('Accept'.'application/json');
}
Copy the code

Data crawling and storage code consolidation

const MongoClient = require('mongodb').MongoClient;
const Parser = require('rss-parser');
const parser = new Parser();
const url = 'mongodb://localhost:27017';
const dbName = 'robot'; // Database name
const collectionName = 'juejin'; // Set name (table name)
const pullList = ['https://rsshub.app/juejin/category/frontend'];

/ / insert
async function insert(db, { title, link, guid }) {
 const collection = db.collection(collectionName);
 // Insert some documents
 try {
   await collection.updateOne(
     {
       guid
     },
     {
       $set: { title, link, guid },
       $setOnInsert: { status: 0}}, {upsert: true}); }catch (err) {
   console.log(err); }}/ / the crawler
async function crawl(url) {
 try {
   const feed = await parser.parseURL(url);
   const items = feed.items.map(({ title, link, guid = link }) = > {
     title = title.trim();
     link = link.trim();
     guid = guid.trim();
     console.log(title, link);
     return { title, link, guid };
   });

   return items;
 } catch (err) {
   console.log(err); }} (async() = > {// Create a new MongoClient
 const client = new MongoClient(url);

 try {
   // Use connect method to connect to the Server
   await client.connect();

   console.log('Connected successfully to server');

   const db = client.db(dbName);

   const promises = pullList.map((value) = > {
     return (async () = > {
       const items = await crawl(value);
       const insertPromises = items.map((item) = > {
         return insert(db, item);
       });

       await Promise.all(insertPromises); }) (); });await Promise.all(promises).then((a)= > {
     client.close();
   });
 } catch (err) {
   console.log(err.stack);
 }
})();
Copy the code

Data push and search code integration

In order to ensure the operation of the code, remember to modify to their own QQ group number (the following is only to send group messages as an example, specific can also be sent private messages, discussion group messages)

const MongoClient = require('mongodb').MongoClient;
const request = require('superagent');
const url = 'mongodb://localhost:27017';
const dbName = 'robot'; // Database name
const collectionName = 'juejin'; // Set name (table name)

/ / to find
async function find(db) {
 const collection = db.collection(collectionName);
 // Find some documents
 try {
   return await collection
     .find({
       status: 0
     })
     .toArray();
 } catch (err) {
   console.log(err); }}/ / update
async function update(db, { guid }) {
 const collection = db.collection(collectionName);
 // Update some documents
 try {
   await collection.updateOne(
     {
       guid
     },
     {
       $set: { status: 1}}); }catch (err) {
   console.log(err); }}// Push groups
async function send(message) {
 return await request
   .post('http://0.0.0.0:5700/send_group_msg')
   .send({ group_id: XXX, message }) // Remember to modify 😊
   .set('Accept'.'application/json');
}

(async() = > {// Create a new MongoClient
 const client = new MongoClient(url);

 try {
   // Use connect method to connect to the Server
   await client.connect();

   console.log('Connected successfully to server');

   const db = client.db(dbName);

   const docs = await find(db);

   console.log(docs);

   let message = ' ';
   message = docs.reduce((acu, { title, link }, index) = > {
     return `${acu}${title} ${link}${index === docs.length - 1 ? ' ' : '\n'}`;
   }, message);

   const { text } = await send(message);
   const { status, retcode } = JSON.parse(text);
   if (status === 'ok' && retcode === 0) {
     const promises = docs.map((value) = > {
       return update(db, value);
     });

     await Promise.all(promises);
   } else {
     console.log(status, retcode);
   }

   client.close();
 } catch (err) {
   console.log(err.stack);
 }
})();
Copy the code

Looking forward to

The above is just a simple application of CoolQ. Recently, there was another idea of Ele. me to calculate the optimal meal according to the discount of merchants, but due to the problem of algorithm, it was temporarily stuck in this part. If you have any other ideas, please share with us

Finally, a bold hypothesis, careful verification, everyone is a scientist 😂, welcome to share your idea!


The text/lastSeries

The author is also in the nuggets oh, quickly follow him!

Sound/fluorspar

This article is authorized by chuangyu front-end author, copyright belongs to the author, chuangyu front-end production. Welcome to indicate the source of this article. Article link: original link

To subscribe for more sharing from the front line of KnownsecFED development, please search our wechat official account KnownsecFED. Welcome to leave a comment to discuss, we will reply as far as possible.

Thank you for reading.