Background
I’ve been following WeChat bots for quite some time. As a frontend developer with solid Node.js knowledge, I wanted to create my own WeChat calculator bot. The reason is simple: every time I need a calculator, it’s very inconvenient. The calculator app on my phone is buried deep in folders, hard to find, and putting it on the home screen feels like a waste of space. So I thought of using Wechaty to implement a personal WeChat calculator bot that can perform basic arithmetic operations like addition, subtraction, multiplication, and division.
Features
Addition, subtraction, multiplication, and division
Implementation Logic
The logic is quite simple: use Wechaty to receive user messages, filter the messages, and when a user inputs something like “1+1”, directly evaluate the expression and return the result to the user.
Dependencies
- wechaty: Core Wechaty library
- wechaty-puppet-padplus: iPad protocol implementation for Wechaty
Implementation Process
function calculator(intxt, callback) {
return new Promise(function (resolve, reject) {
var a = intxt;
try {
intxt = intxt.replace(/=|等|等于|\?/, '');
intxt = intxt.replace(/加/g, '+').replace(/减/g, '-').replace(/乘/g, '*').replace(/除/g, '/');
a = eval(intxt);
} catch(e){
// console.log('========error', e);
}
resolve(a);
});
}
async function onMessage (msg) {
const contact = msg.from()
let text = msg.text()
const room = msg.room();
if (room) return;
if(msg.self()){ // Skip self messages
return;
}
if (text) {
text = text.replace(/[。,、,.]$/gi, '').replace(/\s*/gi, "");
}
if (msg.type() === bot.Message.Type.Text && /^\d+.{1}\d+/gi.test(text)) { // Text messages
let result = await CalcFunc.calculator(text);
await msg.say(result+'');
}
}
Local Setup
- Clone the project
git clone https://github.com/leiroc/wechat-calculator.git
cd wechat-calculator
- Install dependencies
npm install
- Start the project
node app.js
Demo
Acknowledgments
Thanks to the Wechaty team for providing the WeChat bot SDK, allowing developers to focus on business logic. Thanks to Juzi.BOT for providing the Pad protocol token. I saw many DLL-based implementations but decided not to explore them due to time constraints.
中文版: 微信计算器机器人