Skip to content

Commit

Permalink
Merge pull request #125 from wechaty/esm
Browse files Browse the repository at this point in the history
Wechaty v1.x API
  • Loading branch information
padlocal authored May 11, 2022
2 parents 84046e8 + f43b369 commit 13fe8ba
Show file tree
Hide file tree
Showing 50 changed files with 1,318 additions and 1,241 deletions.
14 changes: 14 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

const rules = {
semi: ["error", "always"],
quotes: ["error", "double"],
'space-before-function-paren': ["error", "never"],
}

module.exports = {
extends: '@chatie',
rules,
env: {
jest: true,
},
}
8 changes: 0 additions & 8 deletions .eslintrc.js

This file was deleted.

44 changes: 23 additions & 21 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
name: NPM

# disable actions temporarily
# on: [push, pull_request]
on:
push:
branches:
- NONE
on: [push, pull_request]

jobs:
build:
name: Build
strategy:
matrix:
os: [ubuntu-latest]
node: [12]
os:
- ubuntu-latest
node-version:
- 16

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: npm
cache-dependency-path: package.json

- name: Install Dependencies
run: npm install
Expand All @@ -35,38 +34,41 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: 12
node-version: 16
cache: npm
cache-dependency-path: package.json

- name: Install Dependencies
run: npm install

- name: Generate Version
run: ./scripts/generate-version.sh
- name: Generate Package JSON
run: ./scripts/generate-package-json.sh

- name: Pack Testing
run: ./scripts/npm-pack-testing.sh
env:
WECHATY_PUPPET_HOSTIE_TOKEN: ${{ secrets.WECHATY_PUPPET_HOSTIE_TOKEN }}
NODE_PADLOCAL_TOKEN: ${{ secrets.PADLOCAL_TOKEN }}

publish:
if: github.event_name == 'push' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/v'))
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/v'))
name: Publish
needs: [build, pack]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: 12
node-version: 16
registry-url: https://registry.npmjs.org/

cache: npm
cache-dependency-path: package.json
- name: Install Dependencies
run: npm install

- name: Generate Version
run: ./scripts/generate-version.sh
- name: Generate Package JSON
run: ./scripts/generate-package-json.sh

- name: Set Publish Config
run: ./scripts/package-publish-config-tag.sh
Expand All @@ -77,7 +79,7 @@ jobs:
- name: Check Branch
id: check-branch
run: |
if [[ ${{ github.ref }} =~ ^refs/heads/(master|v[0-9]+\.[0-9]+.*)$ ]]; then
if [[ ${{ github.ref }} =~ ^refs/heads/(main|v[0-9]+\.[0-9]+.*)$ ]]; then
echo ::set-output name=match::true
fi # See: https://stackoverflow.com/a/58869470/1123955
- name: Is A Publish Branch
Expand Down
8 changes: 8 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Wechaty is a Conversational SDK for Chatbot Makers.
Copyright 2020-now PadLocal and Wechaty Community Contributors.

This product includes software developed at
The Wechaty Organization (/~https://github.com/wechaty).

This software contains code derived from the Stackoverflow,
including various modifications by GitHub.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
![Stage](https://img.shields.io/badge/Stage-beta-yellow)

## WECHATY PUPPET NEW STAR

> "New star from our community, which is the third most used Wechaty Puppet Provider now. Thank you very much @padlocal, for creating this WPP & WPS!"
>
>
> — ⭐️ Wechaty [The Trends in March 2021](https://wechaty.js.org/2021/03/04/wechaty-puppet-providers-trends/)
## TL;DR
Expand All @@ -18,17 +19,19 @@
3. Getting Started with Wechaty: <https://wechaty.js.org/docs/getting-started/>

## GETTING STARTED
* STEP 1: [How to Apply Token](/~https://github.com/padlocal/wechaty-puppet-padlocal/wiki/How-to-Apply-Token)
* STEP 2: [Getting Started with TypeScript/JavaScript (RECOMMENDED)](/~https://github.com/padlocal/wechaty-puppet-padlocal/wiki/Getting-Started-with-TypeScript-Javascript)
* STEP 2 or: [Getting Started with Python/Java/Go](/~https://github.com/padlocal/wechaty-puppet-padlocal/wiki/Getting-Started-with-Python-Java-Go)

* STEP 1: [How to Apply Token](/~https://github.com/padlocal/wechaty-puppet-padlocal/wiki/How-to-Apply-Token)
* STEP 2: [Getting Started with TypeScript/JavaScript (RECOMMENDED)](/~https://github.com/padlocal/wechaty-puppet-padlocal/wiki/Getting-Started-with-TypeScript-Javascript)
* STEP 2 or: [Getting Started with Python/Java/Go](/~https://github.com/padlocal/wechaty-puppet-padlocal/wiki/Getting-Started-with-Python-Java-Go)

## REFERENCE
* [API 使用文档 (TypeScript/JavaScript)](/~https://github.com/padlocal/wechaty-puppet-padlocal/wiki/API-%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3-(TypeScript-JavaScript))
* [常见问题列表](/~https://github.com/padlocal/wechaty-puppet-padlocal/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E5%88%97%E8%A1%A8)

* [API 使用文档 (TypeScript/JavaScript)](/~https://github.com/padlocal/wechaty-puppet-padlocal/wiki/API-%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3-(TypeScript-JavaScript))
* [常见问题列表](/~https://github.com/padlocal/wechaty-puppet-padlocal/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E5%88%97%E8%A1%A8)

## PUPPET COMPARISON

PadLocal is _"one of"_ the most powerful puppet yet.
PadLocal is _"one of"_ the most powerful puppet yet.

Puppet|donut|wxwork|paimon|padlocal👍
:---|:---:|:---:|:---:|:---:
Expand Down Expand Up @@ -84,3 +87,11 @@ Puppet|donut|wxwork|paimon|padlocal👍
依赖协议|Windows|Windows|iPad|iPad

> [Wechaty puppet compatibility](/~https://github.com/wechaty/wechaty-puppet/wiki/Compatibility)
## Author

[@PadLocal](/~https://github.com/padlocal)

## License

Apache-2.0
67 changes: 37 additions & 30 deletions examples/demo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// read config from local-test.json
process.env.NODE_CONFIG_ENV = "test";

import { MessageType } from "wechaty-puppet";
import { Contact, log, Message, ScanStatus, Wechaty } from "wechaty";
import PuppetPadlocal from "../src/puppet-padlocal";
import * as PUPPET from "wechaty-puppet";
import { Contact, log, Message, ScanStatus, WechatyBuilder } from "wechaty";
import PuppetPadlocal from "../src/puppet-padlocal.js";
import config from "config";
import QRCode from "qrcode-terminal";
import { FileBoxJsonObjectUrl } from "file-box/src/file-box.type";
import { isContactId } from "../src/padlocal/utils/is-type";
import { isContactId } from "../src/padlocal/utils/is-type.js";

// log.level("silly");

Expand All @@ -18,44 +14,47 @@ async function getMessagePayload(message: Message) {
const recallUserId: string = config.get("test.push.recallUserId");

switch (message.type()) {
case MessageType.Text:
if (message.talker()?.id === recallUserId && message.text()!.indexOf("recall") !== -1) {
case PUPPET.types.Message.Text:
if (message.talker().id === recallUserId && message.text()!.indexOf("recall") !== -1) {
await message.recall();
}
break;

case MessageType.Attachment:
case MessageType.Audio:
case PUPPET.types.Message.Attachment:
case PUPPET.types.Message.Audio: {
const attachFile = await message.toFileBox();

const dataBuffer = await attachFile.toBuffer();

log.info("TestBot", `get message audio or attach: ${dataBuffer.length}`);

break;
}

case MessageType.Video:
case PUPPET.types.Message.Video: {
const videoFile = await message.toFileBox();

const videoData = await videoFile.toBuffer();

log.info("TestBot", `get message video: ${videoData.length}`);

break;
}

case MessageType.Emoticon:
case PUPPET.types.Message.Emoticon: {
const emotionFile = await message.toFileBox();

const emotionJSON = emotionFile.toJSON() as FileBoxJsonObjectUrl;
const emotionJSON = emotionFile.toJSON();
log.info("TestBot", `get message emotion json: ${JSON.stringify(emotionJSON)}`);

const emotionBuffer: Buffer = await emotionFile.toBuffer();

log.info("TestBot", `get message emotion: ${emotionBuffer.length}`);

break;
}

case MessageType.Image:
case PUPPET.types.Message.Image: {
const messageImage = await message.toImage();

const thumbImage = await messageImage.thumbnail();
Expand All @@ -74,8 +73,9 @@ async function getMessagePayload(message: Message) {
log.info("TestBot", `get message image, artwork: ${artworkImageData.length}`);

break;
}

case MessageType.Url:
case PUPPET.types.Message.Url: {
const urlLink = await message.toUrlLink();
log.info("TestBot", `get message url: ${JSON.stringify(urlLink)}`);

Expand All @@ -85,26 +85,29 @@ async function getMessagePayload(message: Message) {
log.info("TestBot", `get message url thumb: ${urlThumbImageData.length}`);

break;
}

case MessageType.MiniProgram:
case PUPPET.types.Message.MiniProgram: {
const miniProgram = await message.toMiniProgram();

log.info(`MiniProgramPayload: ${JSON.stringify(miniProgram)}`);

break;
}
}
}

const bot = new Wechaty({
const bot = WechatyBuilder.build({
name: "TestBot",
puppet,
})
});

bot
.on("scan", (qrcode: string, status: ScanStatus) => {
if (status === ScanStatus.Waiting && qrcode) {
log.info(
"TestBot",
`onScan: ${ScanStatus[status]}(${status})\n\n ▼▼▼ Please scan following qr code to login ▼▼▼\n`
`onScan: ${ScanStatus[status]}(${status})\n\n ▼▼▼ Please scan following qr code to login ▼▼▼\n`,
);

QRCode.generate(qrcode, { small: true });
Expand All @@ -117,25 +120,25 @@ const bot = new Wechaty({
log.info("TestBot", `${user} login`);
})

.on("logout", (user: Contact, reason: string) => {
.on("logout", (user, reason) => {
log.info("TestBot", `${user} logout, reason: ${reason}`);
})

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

const forwardFrom = config.get("test.push.forwardFrom");
const forwardTo: string = config.get("test.push.forwardTo");

if (message.type() === MessageType.Text) {
if (message.type() === PUPPET.types.Message.Text) {
// ding-dong bot
if (message.to()?.self() && message.text().indexOf("ding") !== -1) {
await message.talker().say(message.text().replace("ding", "dong"));
}
}

if (message.talker()?.id === forwardFrom) {
if (message.type() === MessageType.Unknown) {
if (message.talker().id === forwardFrom && message.listener()?.id === forwardFrom) {
if (message.type() === PUPPET.types.Message.Unknown) {
return;
}

Expand All @@ -149,7 +152,7 @@ const bot = new Wechaty({
const newMessage = await message.forward(to!);
await getMessagePayload(newMessage as Message);
} catch (e) {
log.error("TestBot", `Error while forwarding message: ${e.stack}`);
log.error("TestBot", `Error while forwarding message: ${(e as Error).stack}`);
}
}

Expand All @@ -160,6 +163,10 @@ const bot = new Wechaty({
log.info("TestBot", `on error: ${error.toString()}`);
});

bot.start().then(() => {
log.info("TestBot", "started.");
});
bot
.start()
.then(() => {
log.info("TestBot", "started.");
return null;
})
.catch(console.error);
5 changes: 5 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: "ts-jest",
setupFilesAfterEnv: ["./jest.setup.ts"],
resolver: "jest-ts-webcompat-resolver",
};
7 changes: 0 additions & 7 deletions jest.config.js

This file was deleted.

Loading

0 comments on commit 13fe8ba

Please sign in to comment.