Skip to content

Commit

Permalink
PuppetPadchat refactoring... (#1249)
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Jun 3, 2018
1 parent 34a0f53 commit 2ba76a8
Show file tree
Hide file tree
Showing 19 changed files with 668 additions and 463 deletions.
20 changes: 11 additions & 9 deletions examples/ding-dong-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const BOT_QR_CODE_IMAGE_FILE = path.resolve(
'../docs/images/bot-qr-code.png',
)

const bot = Wechaty.instance()

const welcome = `
| __ __ _ _
| \\ \\ / /__ ___| |__ __ _| |_ _ _
Expand All @@ -49,6 +51,7 @@ const welcome = `
=============== Powered by Wechaty ===============
-------- https://github.com/chatie/wechaty --------
Version: ${bot.version(true)}
I'm a bot, my superpower is talk in Wechat.
Expand All @@ -63,22 +66,21 @@ Please wait... I'm trying to login in...
`

console.log(welcome)
const bot = Wechaty.instance()

bot
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
.on('login' , user => {
log.info('Bot', `${user.name()} login`)
bot.say('Wechaty login').catch(console.error)
})
.on('scan', (qrCode, statusCode) => {
if (!/201|200/.test(String(statusCode))) {
QrcodeTerminal.generate(qrCode, { small: true }, (qrcode: string) => {
console.log(qrcode)
console.log(qrCode)
console.log(`[${statusCode}] Scan QR Code above url to log in: `)
})
}
.on('scan', (qrData, status, data) => {
QrcodeTerminal.generate(qrData, { small: true }, (asciiArt: string) => {
console.log(asciiArt)
console.log(`[${status}] Scan QR Code above url to log in: `)
if (data) {
console.log(data)
}
})
})
.on('message', async msg => {
try {
Expand Down
5 changes: 3 additions & 2 deletions examples/self-testing-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const BOT_QR_CODE_IMAGE_FILE = path.resolve(
'../docs/images/bot-qr-code.png',
)

const bot = Wechaty.instance()

const welcome = `
| __ __ _ _
| \\ \\ / /__ ___| |__ __ _| |_ _ _
Expand All @@ -51,6 +53,7 @@ const welcome = `
=============== Powered by Wechaty ===============
-------- https://github.com/chatie/wechaty --------
Version: ${bot.version(true)}
I'm a bot, my superpower is talk in Wechat.
Expand All @@ -65,7 +68,6 @@ Please wait... I'm trying to login in...
`

console.log(welcome)
const bot = Wechaty.instance()

bot
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
Expand All @@ -83,7 +85,6 @@ bot
})
})
.on('message', async msg => {
console.log('on message:')
try {
console.log(msg.toString()) // on(message) exception: Error: no file
const text = msg.text()
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"normalize-package-data": "^2.4.0",
"promise-retry": "^1.1.1",
"puppeteer": "^1.2.0",
"qr-image": "^3.2.0",
"raven": "^2.6.2",
"read-pkg-up": "^3.0.0",
"request": "^2.87.0",
Expand All @@ -134,6 +135,7 @@
"@types/normalize-package-data": "^2.4.0",
"@types/promise-retry": "^1.1.1",
"@types/puppeteer": "^1.0.0",
"@types/qr-image": "^3.2.1",
"@types/raven": "^2.1.0",
"@types/read-pkg-up": "^3.0.0",
"@types/request": "^2.0.0",
Expand Down
18 changes: 17 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import * as path from 'path'

import * as readPkgUp from 'read-pkg-up'
import * as Raven from 'raven'
import { log } from 'brolog'
import * as qrImage from 'qr-image'

import { log } from 'brolog'
import {
FileBox,
} from 'file-box'

import {
PuppetName,
Expand Down Expand Up @@ -189,6 +194,17 @@ export class Config {
}
}

export const CHATIE_OFFICIAL_ACCOUNT_ID = 'gh_051c89260e5d'

export function qrCodeForChatie(): FileBox {
const CHATIE_OFFICIAL_ACCOUNT_QRCODE = 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
const name = 'qrcode-for-chatie.png'
const type = 'png'

const qrStream = qrImage.image(CHATIE_OFFICIAL_ACCOUNT_QRCODE, { type })
return FileBox.fromStream(qrStream, name)
}

export interface Sayable {
say(text: string, replyTo?: any|any[]): Promise<void>
}
Expand Down
2 changes: 1 addition & 1 deletion src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class Message extends Accessory implements Sayable {
')',
]
if (this.type() === Message.Type.Text) {
msgStrList.push(`<${this.text()}>`)
msgStrList.push(`<${this.text().substr(0, 70)}>`)
} else {
log.silly('Message', 'toString() for message type: %s(%s)', Message.Type[this.type()], this.type())

Expand Down
13 changes: 13 additions & 0 deletions src/puppet-mock/puppet-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {

import {
log,
qrCodeForChatie,
} from '../config'

export type PuppetFoodType = 'scan' | 'ding'
Expand Down Expand Up @@ -284,6 +285,18 @@ export class PuppetMock extends Puppet {
log.verbose('PuppetMock', 'roomDel(%s, %s)', roomId, contactId)
}

public async roomAvatar(roomId: string): Promise<FileBox> {
log.verbose('PuppetMock', 'roomAvatar(%s)', roomId)

const payload = await this.roomPayload(roomId)

if (payload.avatar) {
return FileBox.fromUrl(payload.avatar)
}
log.warn('PuppetMock', 'roomAvatar() avatar not found, use the chatie default.')
return qrCodeForChatie()
}

public async roomAdd(
roomId : string,
contactId : string,
Expand Down

0 comments on commit 2ba76a8

Please sign in to comment.