Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wechaty v1.x API #125

Merged
merged 43 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
83aa331
ES Module & Wechaty v1.x API
huan Jan 18, 2022
2597f27
clean
huan Jan 18, 2022
53b8463
0.4.3
huan Jan 18, 2022
1c1cbde
code clean
huan Jan 18, 2022
c667c53
0.4.4
huan Jan 18, 2022
e75733e
v1.11
huan Jan 18, 2022
89082a9
1.11.2
huan Jan 18, 2022
892dfd4
fix typo
huan Jan 18, 2022
9a10bbe
1.11.3
huan Jan 18, 2022
bf32dd5
fix some esm for running dmeo
huan Jan 23, 2022
d38445a
fix linting
huan Jan 23, 2022
d1874bd
1.11.4
huan Jan 23, 2022
c7a8be5
disable ESM
huan Feb 5, 2022
beb8a48
1.11.5
huan Feb 5, 2022
9e89549
add resolver and fix tsconfig
huan Feb 6, 2022
81a87a4
1.11.6
huan Feb 6, 2022
94ede06
add a runnable tap unit test (#125)
huan Feb 22, 2022
593c615
1.11.7
huan Feb 22, 2022
6f050cb
remove .js from import
huan Feb 27, 2022
78131d5
update demo code
padlocal Feb 27, 2022
285ef67
adapt to new Wechaty api changes
padlocal Apr 28, 2022
ea90997
update jest version, to make test works
padlocal Apr 28, 2022
2190ddf
Fix issue #115
padlocal Apr 28, 2022
0c0770b
fix bug #112
padlocal Apr 29, 2022
2900d0e
fix bug #91
padlocal Apr 29, 2022
0e8456c
fix bug #102
padlocal Apr 29, 2022
2b54aee
fix bug #99
padlocal Apr 30, 2022
9e2ce61
Adapt to Wechaty puppet API V1.x.
padlocal Apr 30, 2022
740706b
fix bug #101
padlocal Apr 30, 2022
7fa2839
upgrade to esm and cjs dual module
padlocal May 8, 2022
8c94c14
use package version as puppet padlocal version
padlocal May 8, 2022
d08f5b1
fix lint issue
padlocal May 8, 2022
154b49c
1.11.8
padlocal May 8, 2022
6a6a6ca
add smoke test scripts for github actions
padlocal May 8, 2022
ce77749
1.11.9
padlocal May 8, 2022
06ab87f
update demo code
padlocal May 11, 2022
1dbe549
update emoticon message FileBox data structure.
padlocal May 11, 2022
10842a8
1.11.10
padlocal May 11, 2022
4588008
fix exception raised by url message without thumb
padlocal May 11, 2022
e2e4f0f
fix github action error caused by eslint
padlocal May 11, 2022
bdc8c0b
1.11.11
padlocal May 11, 2022
d287740
set timeout up to 60 secs for smoke test
padlocal May 11, 2022
f43b369
1.11.12
padlocal May 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,22 +7,25 @@
![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/)

## 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 @@ -78,3 +81,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