Skip to content

Getting Started with TypeScript Javascript

padlocal edited this page May 7, 2021 · 4 revisions

There are some ways to get started with PadLocal.

Quick Start with Demo

0. Apply Token

You need PadLocal token to run this demo. How to Apply Token

1. Prepare Node Environment

Install Node, 12/14 LTS version is recommended.

$ node --version // >= v12.0.0

2. Clone the wechaty-puppet-padlocal-demo project.

$ git clone git@github.com:padlocal/wechaty-puppet-padlocal-demo.git

Then install Node dependencies.

$ cd wechaty-puppet-padlocal-demo
$ npm install

3. Set you PadLocal Token in main.ts

const puppet = new PuppetPadlocal({
    token: "YOUR_PADLOCAL_TOKEN"
})

Then run it:

$ npm run demo

carbon

Getting Started from Scratch: step-by-step instructions

If you want to explore PadLocal step by step, following instructions may be helpful.

0. Apply Token

You need PadLocal token to run PadLocal. How to Apply Token

1. Prepare Node environment

Install Node, 12/14 LTS version is recommended.

$ node --version // >= v12.0.0

2. Create and init your bot project

mkdir my-padlocal-bot && cd my-padlocal-bot
npm init -y
npm install ts-node typescript -g --registry=https://r.npm.taobao.org
tsc --init --target ES6

3. Install Wechaty and Padlocal puppet

npm install wechaty@latest --registry=https://r.npm.taobao.org
npm install wechaty-puppet-padlocal@latest --registry=https://r.npm.taobao.org

4. Write bot demo code

// bot.ts

import {PuppetPadlocal} from "wechaty-puppet-padlocal";
import {Contact, Message, ScanStatus, Wechaty} from "wechaty";

const token: string = ""            // padlocal token
const puppet = new PuppetPadlocal({ token })

const bot = new Wechaty({
    name: "TestBot",
    puppet,
})

bot
.on("scan", (qrcode: string, status: ScanStatus) => {
    if (status === ScanStatus.Waiting && qrcode) {
        const qrcodeImageUrl = ["https://api.qrserver.com/v1/create-qr-code/?data=", encodeURIComponent(qrcode)].join("");
        console.log(`onScan: ${ScanStatus[status]}(${status}) - ${qrcodeImageUrl}`);
    } else {
        console.log(`onScan: ${ScanStatus[status]}(${status})`);
    }
})

.on("login", (user: Contact) => {
    console.log(`${user} login`);
})

.on("logout", (user: Contact) => {
    console.log(`${user} logout`);
})

.on("message", async (message: Message) => {
    console.log(`on message: ${message.toString()}`);
})

.start()

console.log("TestBot", "started");

Then run the bot:

$ ts-node bot.ts