Suck the cat with code! This paper is participating in[Cat Essay Campaign].

preface

What if there’s no cat? Get yourself a new Cat and masturbate slowly. How lu? Based on the recent research on the enterprise wechat third-party application, then build a “self-discipline cat”. Let it remind me of my to-do list every time.

The overall design

System Architecture Diagram

Content: By visiting the wechat mini program “Self-discipline Cat” service, submit daily to-do events, self-discipline cat service will be pushed to catHook service, so that the catHook service can call back the message sending function of “Self-discipline Cat” service and push the message to the wechat mini program.

Requirements:

1. Users can submit their daily tasks, including type, event content and reminder time, by opening the small program, yimeow every day today

2. Cathook, as the dispatching center, not only receives all the information, but also can trigger the reminder when it arrives

3. SelfCat Server pushes messages to the wechat of the enterprise

The detailed design

SelfCat-Server

Provides front-end page and related service interface, technology stack: NodeJS + Koa

1. The user’s WXUID is obtained according to accessToken when the user clicks the page

2. After the front-end user submits the task to be reminded, the task will be forwarded to CatHook scheduling center for unified scheduling

3. Provide a message sending interface, which is invoked when the dispatch center triggers message push and pushes tasks to users at the time point

Messages are sent mainly by using encapsulated third-party module, Weike-Enterprise:

var API = require("wechat-enterprise").API; var api = new API(null, null, cfg.agentid, function (callback) { callback(null, { accessToken: token, }); api.send(postData.to, postData.message, function (err, Value) {if (err) {if (err. Code && err. Code === 81013) {logger.warn(' 【warn】 no user found, error message: ${err},${JSON.stringify(data)}`); callback(null, value); } else {logger.debug(' error message: ${err} '); } } callback(err, value); });Copy the code

CatHook-Server

It mainly provides scheduling tasks and receiving scheduling tasks. Technology stack: NodeJS + Express + mongodb

1. Use the node-schedule module to start scheduled tasks.

2. Provides an interface for starting scheduled tasks, supports one-time scheduled reminder or regular scheduled reminder functions, and stores the received scheduled tasks to mongodb.

3. Provides an interface for shutting down scheduled tasks to meet requirements for canceling scheduled tasks.

Here is the method code for the scheduled task interface:

Function schedule_start(execModel) {var jobId = execmodel.jobid; schedules.scheduleJob(jobId, rule, () => { schServer.callWebhookSchedule(jobId, webhook, false).then(result => { sendMessage(`${config.appName}${result.msg}`, null); }).catch(errMsg => { sendMessage(`${config.appName}${errMsg}`, null); }); }); } function callWebhookSchedule (jobId, webhook,isOnce) {return reqserver.callapi (webhook). Then (result => {let MSG = ""; If (result.errcode === 0) {MSG = 'jobCode:${jobId},url :${webhook}; logger.info(msg); If (isOnce){deleteDBOverJob(jobId). Catch (errMsg => {return promise.reject (errMsg); // If (isOnce){deleteDBOverJob(jobId). }); } return Promise.resolve({code:0,msg:msg}); } else {MSG = 'jobCode:${jobId},url :${webhook} error :${JSON. Stringify (result)}'; logger.error(msg); return Promise.reject(msg); }}). Catch (err => {let errMsg = 'task ${jobId}, call interface url :${webhook} exception :${err}' logger.error(errMsg); return Promise.reject(errMsg); })}Copy the code

Results show

It seems to be a very simple function, but it has been almost knocking on the code for more than ten days, which is only part of the completion of the function. It is deployed to my cloud service for trial operation.

The last words

The purpose of this cat is too simple, just to remind me to do things. How do I know if I’m cured of procrastination? Next, I will continue to improve and add features: the ratio of each time I complete a task to the processing time, so that I can make a weekly review of how long IT takes me to complete a task.

It’s not the alarm clock that wakes me up every day, but self-discipline meow!!

Come on!!