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

Update wechaty-puppet-oa #24

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
]
}
]
}
}
34 changes: 29 additions & 5 deletions examples/ding-dong-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
EventScanPayload,
EventErrorPayload,
EventMessagePayload,
FileBox
FileBox,
MessageType,
} from 'wechaty-puppet'

import { PuppetOA } from '../src/mod'
Expand All @@ -32,7 +33,13 @@ import { PuppetOA } from '../src/mod'
* 1. Declare your Bot!
*
*/
const puppet = new PuppetOA()
const puppet = new PuppetOA({
  appId           : '',
  appSecret       : '',
  token           : '',
  webhookProxyUrl : '',
  // port:8080
})

/**
*
Expand Down Expand Up @@ -113,13 +120,30 @@ async function onMessage (payload: EventMessagePayload) {
console.info('onMessage:', JSON.stringify(msgPayload))
if (/ding/i.test(msgPayload.text || '')) {
await puppet.messageSendText(msgPayload.fromId!, 'dong')
}
else if (/image/i.test(msgPayload.text || '')) {
let fileBox = FileBox.fromUrl("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1116676390,2305043183&fm=26&gp=0.jpg","ding-dong.jpg")
} else if (/hi|hello/i.test(msgPayload.text || '')) {
const _userinfo = await puppet.contactRawPayload(msgPayload.fromId!)
await puppet.messageSendText(msgPayload.fromId!, 'hello,' + _userinfo.nickname + '. Thanks for your attention')
} else if (/image/i.test(msgPayload.text || '')) {
const fileBox = FileBox.fromUrl("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1116676390,2305043183&fm=26&gp=0.jpg", "ding-dong.jpg")
if (msgPayload.fromId){
await puppet.messageSendFile(msgPayload.fromId!, fileBox)
}
} else if (msgPayload.type == MessageType.Image){
// const imageFile = await puppet.messageFile(msgPayload.fromId!)
const imageFile = FileBox.fromUrl(msgPayload.filename + '.jpg')
// console.log(image)
if (msgPayload.fromId!){
await puppet.messageSendFile(msgPayload.fromId!, imageFile)
}
}
// } else if (/获取好友列表/i.test(msgPayload.text || '')) {
// const _contactList = await puppet.contactList()
// if (_contactList != null) {
// for (const i in _contactList) {
// console.info(i)
// await puppet.messageSendText(msgPayload.fromId!, _contactList[i])
// }
// }
}

/**
Expand Down
26 changes: 22 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-official-account",
"version": "0.5.46",
"version": "0.5.52",
"description": "Wechaty Puppet for WeChat Official Accounts",
"directories": {
"test": "tests"
Expand Down Expand Up @@ -54,20 +54,22 @@
"@types/express": "^4.17.11",
"@types/express-xml-bodyparser": "^0.3.2",
"@types/localtunnel": "^2.0.0",
"@types/lru-cache": "^5.1.1",
"@types/node": "^16.4.1",
"@types/normalize-package-data": "^2.4.0",
"@types/quick-lru": "^3.0.0",
"ci-info": "^3.1.0",
"pkg-jq": "^0.2.11",
"shx": "^0.3.3",
"ts-node": "^10.1.0",
"tstest": "^0.4.10",
"wechaty": "^0.56.5",
"wechaty-puppet": "^0.34.1"
"wechaty": "^0.56.6",
"wechaty-puppet": "^0.34.3"
},
"peerDependencies": {
"wechaty-puppet": "*"
},
"dependencies": {
"@types/lru-cache": "^5.1.0",
"@types/uuid": "^8.3.0",
"cuid": "^2.1.8",
"express": "^4.17.1",
Expand All @@ -89,5 +91,21 @@
"scripts": {
"pre-push": "npx git-scripts-pre-push"
}
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"rules": {
"no-console": "off"
},
"parserOptions": {
"parser": "babel-eslint"
}
}
}
3 changes: 1 addition & 2 deletions src/official-account/official-account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ test('OfficialAccount smoke testing', async (t) => {
'.localtunnel.chatie.io',
// '.test.localhost.localdomain',
].join('')

const oa = new OfficialAccount({
...getOaOptions(),
webhookProxyUrl : WEBHOOK_PROXY_URL,
Expand Down Expand Up @@ -72,7 +71,7 @@ test('OfficialAccount smoke testing', async (t) => {
try {
await Promise.race([
future,
new Promise((resolve, reject) => resolve && setTimeout(reject, 15000)),
new Promise((resolve, reject) => !!resolve && setTimeout(reject, 15000)),
])
t.pass('should get a message emit event from oa instance')
} catch (e) {
Expand Down
4 changes: 3 additions & 1 deletion src/official-account/official-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class OfficialAccount extends EventEmitter {

// keep the official account id consist with puppet-oa
this.oaId = `gh_${options.appId}`
// this.oaId = `${options.appId}`

this.webhook = new Webhook({
personalMode : !!this.options.personalMode,
Expand Down Expand Up @@ -222,7 +223,7 @@ class OfficialAccount extends EventEmitter {
*/
let timer: undefined | ReturnType<typeof setTimeout>

const update = () => this.updateAccessToken()
const update = ():any => this.updateAccessToken()
.then(succeed => succeed
? this.accessTokenPayload!.expiresIn - marginSeconds
: tryAgainSeconds
Expand Down Expand Up @@ -399,6 +400,7 @@ class OfficialAccount extends EventEmitter {
log.verbose('OfficialAccount', 'getContactPayload(%s)', openId)

if (openId && openId.startsWith('gh_')) {
// if (openId) {

// wechaty load the SelfContact object, so just return it.
/* eslint-disable sort-keys */
Expand Down
15 changes: 8 additions & 7 deletions src/puppet-oa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class PuppetOA extends Puppet {
throw new Error('Method not implemented.')
}

public static readonly VERSION = VERSION
public static override readonly VERSION = VERSION

protected appId : string
protected appSecret : string
Expand Down Expand Up @@ -151,7 +151,7 @@ class PuppetOA extends Puppet {
}
}

public async start (): Promise<void> {
public override async start (): Promise<void> {
log.verbose('PuppetOA', 'start()')
if (this.state.on()) {
log.warn('PuppetOA', 'start() is called on a ON puppet. await ready(on) and return.')
Expand Down Expand Up @@ -192,7 +192,7 @@ class PuppetOA extends Puppet {
oa.on('logout', _ => this.emit('logout', { contactId: this.id || '', data: 'logout' }))
}

public async stop (): Promise<void> {
public override async stop (): Promise<void> {
log.verbose('PuppetOA', 'stop()')

if (this.state.off()) {
Expand All @@ -215,13 +215,13 @@ class PuppetOA extends Puppet {
}
}

public login (contactId: string): Promise<void> {
public override login (contactId: string): Promise<void> {
log.verbose('PuppetOA', 'login()')
// developer can set contactId
return super.login(contactId)
}

public async logout (): Promise<void> {
public override async logout (): Promise<void> {
log.verbose('PuppetOA', 'logout()')

if (!this.id) {
Expand All @@ -240,7 +240,7 @@ class PuppetOA extends Puppet {
setTimeout(() => this.emit('dong', { data: data || '' }), 1000)
}

public unref (): void {
public override unref (): void {
log.verbose('PuppetOA', 'unref()')
super.unref()
}
Expand Down Expand Up @@ -520,6 +520,7 @@ class PuppetOA extends Puppet {
throw new Error('can"t send personal CustomeMessage')
}
} else {
// msgId = await this.oa?.autoreplyCustomMessage(payload)
msgId = await this.oa?.sendCustomMessage(payload)
}
} else if (something instanceof FileBox) {
Expand Down Expand Up @@ -586,7 +587,7 @@ class PuppetOA extends Puppet {
// return this.messageSend(conversationId, miniProgram)
}

public async messageForward (
public override async messageForward (
conversationId: string,
messageId : string,
): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "@chatie/tsconfig",
"compilerOptions": {
"outDir": "dist",
"lib": ["DOM","es5", "es2015.promise" ,"es2015", "es2017"]
},
"exclude": [
"node_modules/",
Expand Down