From a1c42e091e81bd1410119c338c6adfbed1cadc0f Mon Sep 17 00:00:00 2001 From: NickWang Date: Thu, 10 Aug 2023 19:23:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20new=20api=20overview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docusaurus/docs/api/overview.mdx | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/docusaurus/docs/api/overview.mdx b/docusaurus/docs/api/overview.mdx index 75eb23e6540..959c294fe1e 100644 --- a/docusaurus/docs/api/overview.mdx +++ b/docusaurus/docs/api/overview.mdx @@ -1,6 +1,6 @@ --- slug: /api/ -title: Wechaty API +title: Wechaty API sidebar_label: 'API: Overview' --- @@ -10,16 +10,33 @@ export const Toc = () => -## Wechaty API +## Intro -API- Application programming interface is a software interface that offers services to other software services. Wechaty API is a very tiny and easy to import API that allows users to use all the functionalities of Wechaty to its best. The API offers a variety of user classes, typedefs, functions, methods, and many more features. This section is an overview of the Wechaty API. +Wechaty is a Node.js library for building chatbots that work on WeChat, WhatsApp, and other messaging platforms. With Wechaty, you can build intelligent chatbots that automate tasks, engage with customers, and provide a personalized experience for your users. -## Importing the Wechaty API +With the help of wechaty, you can build chatbots with 6 lines of code. -You can import the Wechaty API as shown below: +### Install -## ES6/TypeScript +In most cases, you should install ```wechaty``` only. The package ```wechaty``` has ```wechaty-puppet```, ```wechaty-puppet-service``` in its dependencies, so you don't have install them by yourself. So all you have to is -```js +```bash +npm install wechaty +``` + +If you wish to use a particular version of ```wechaty-puppet``` or ```wechaty-puppet-service```, you can install such package manually. + +### Run + +```ts import { WechatyBuilder } from 'wechaty' + +async function main () { + const bot = new WechatyBuilder.build() + bot + .on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`)) + .on('login', user => console.log(`User ${user} logged in`)) + .on('message', message => console.log(`Message: ${message}`)) + await bot.start() +} ``` From 7eb0ac5b5689a27c780bb2119822f5eb75a2f126 Mon Sep 17 00:00:00 2001 From: NickWang Date: Mon, 29 Jan 2024 16:27:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20hello=20world=20sect?= =?UTF-8?q?ion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docusaurus/docs/api/hello-world.md | 40 ++++++++++++++++++++++++++++++ docusaurus/sidebars.ts | 1 + 2 files changed, 41 insertions(+) create mode 100644 docusaurus/docs/api/hello-world.md diff --git a/docusaurus/docs/api/hello-world.md b/docusaurus/docs/api/hello-world.md new file mode 100644 index 00000000000..5e5f49194f6 --- /dev/null +++ b/docusaurus/docs/api/hello-world.md @@ -0,0 +1,40 @@ +--- +title: Hello World +sidebar_label: 'Hello World' +--- + +## Hello World + +In this section, we'll create a simple bot: Ding-Dong bot. When it receives a text message 'ding', it will reply with text 'dong'. + +1. install dependencies with npm + + ```bash + npm install wechaty + ``` + +2. create a bot instance + + ```ts + import { WechatyBuilder } from 'wechaty' + + const bot = new WechatyBuilder.build() + ``` + +3. listen to scan event + + ```ts + bot.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`)) + ``` + + As we know, each QRcode represents a particular string. Here the call back of scan event will pass a qrcode string. We generate the qrcode with wechaty website api and scan to login. + +4. listen to message event + + ```ts + bot.on('message', message => if (message.text() === 'ding') { + message.say('dong') + }) + ``` + + The message event will pass a ```MessageInterface``` instance to the callback. We can get the text content by calling ```message.text()```. If the message is 'ding', we reply with ```message.say()```. This is a shortcut for sending messages to the sender of the message. You can find detail info about it in [User-Module: Message](api/message.md) section. diff --git a/docusaurus/sidebars.ts b/docusaurus/sidebars.ts index 9677b00d30a..08a81ab1dfe 100644 --- a/docusaurus/sidebars.ts +++ b/docusaurus/sidebars.ts @@ -22,6 +22,7 @@ const api: SubMenuData = { label: 'API', items: [ 'api/overview', + 'api/hello-world', 'api/wechaty', 'api/message', 'api/contact',