Skip to content

Commit

Permalink
show qrcode in console in examples. support more html emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Jun 12, 2016
1 parent 8951e7e commit b3aeb8b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions example/ding-dong-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ bot
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
.on('scan', ({url, code}) => {
console.log(`Scan QR Code in url to login: ${code}\n${url}`)
if (!/201|200/.test(code)) {
let loginUrl = url.replace(/\/qrcode\//, '/l/')
require('qrcode-terminal').generate(loginUrl)
}
})
.on('message', m => {
m.ready()
Expand Down
3 changes: 2 additions & 1 deletion package.json
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty",
"version": "0.1.3",
"version": "0.1.4",
"description": "Wechat for Bot. (Personal Account, NOT Official Account)",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -59,6 +59,7 @@
"apiai": "^2.0.5",
"cross-env": "^1.0.8",
"eslint": "^2.9.0",
"qrcode-terminal": "^0.10.0",
"tap": "^5.7.1",
"tuling123-client": "0.0.1"
}
Expand Down
5 changes: 3 additions & 2 deletions src/html-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ function unescapeHtml(str) {
}

function digestEmoji(html) {
// <img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />
return html && html
.replace(/<img class="(\w*?emoji) (\w*?emoji[^"]+?)" text="(.*?)_web" src=[^>]+>/g
, '$3')
, '$3') // <img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />
.replace(/<span class="(\w*?emoji) (\w*?emoji[^"]+?)"><\/span>/g
, '[$2]') // '<span class="emoji emoji1f334"></span>'
}

function plainText(html) {
Expand Down
9 changes: 9 additions & 0 deletions src/puppet-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ class PuppetWeb extends Puppet {
this.onBrowserDead(err)
}, TIMEOUT)
this.watchDogTimer.unref() // dont block quit

const SAVE_SESSION_INTERVAL = 5 * 60 * 1000 // 5 min
if (this.session) {
if (!this.watchDogLastSaveSession || Date.now() - this.watchDogLastSaveSession > SAVE_SESSION_INTERVAL) {
log.verbose('PuppetWeb', 'watchDog() saveSession(%s) after %d minutes', this.session, Math.floor(SAVE_SESSION_INTERVAL/1000/60))
this.browser.saveSession(this.session)
this.watchDogLastSaveSession = Date.now()
}
}
}

onServerDing(data) {
Expand Down
2 changes: 1 addition & 1 deletion src/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Room {
} else if (this.obj.members && this.obj.members.length) {
return Promise.resolve(this)
} else if (this.obj.id) {
log.warn('Room', 'ready() on a already ready but members list is empty room. reload')
log.warn('Room', 'ready() ready but members list empty in room %s. reloading', this.obj.name)
}

contactGetter = contactGetter || Room.puppet.getContact.bind(Room.puppet)
Expand Down
2 changes: 1 addition & 1 deletion src/wechaty.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Wechaty extends EventEmitter {
const okPort = yield this.getPort(this.options.port)

if (okPort != this.options.port) {
log.verbose('Wechaty', 'port: %d not available, changed to %d', this.options.port, okPort)
log.info('Wechaty', 'port: %d not available, changed to %d', this.options.port, okPort)
this.options.port = okPort
} else {
log.verbose('Wechaty', 'port: %d', this.options.port)
Expand Down
17 changes: 12 additions & 5 deletions test/html-util-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ test('HtmlUtil smoking test', function(t) {
const HTML_BEFORE_UNESCAPE = '&apos;|&quot;|&gt;|&lt;|&amp;'
const HTML_AFTER_UNESCAPE = `'|"|>|<|&`

const EMOJI_BEFORE_DIGEST = '<img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />'
const EMOJI_AFTER_DIGEST = '[流汗]'
const EMOJI_BEFORE_DIGEST = [
'<img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />'
, '<span class="emoji emoji1f334"></span>'
]
const EMOJI_AFTER_DIGEST = [
'[流汗]'
, '[emoji1f334]'
]

const PLAIN_BEFORE = '&amp;<html>&amp;</html>&amp;<img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />'
const PLAIN_AFTER = '&&&[流汗]'
Expand All @@ -24,9 +30,10 @@ test('HtmlUtil smoking test', function(t) {
const unescapedHtml = htmlUtil.unescapeHtml(HTML_BEFORE_UNESCAPE)
t.equal(unescapedHtml, HTML_AFTER_UNESCAPE, 'should unescape html as expected')

const emojiDigest = htmlUtil.digestEmoji(EMOJI_BEFORE_DIGEST)
t.equal(emojiDigest, EMOJI_AFTER_DIGEST, 'should digest emoji as expected')

for (let i=0; i<EMOJI_BEFORE_DIGEST.length; i++) {
const emojiDigest = htmlUtil.digestEmoji(EMOJI_BEFORE_DIGEST[i])
t.equal(emojiDigest, EMOJI_AFTER_DIGEST[i], 'should digest emoji string ' + i + ' as expected')
}
const plainText = htmlUtil.plainText(PLAIN_BEFORE)
t.equal(plainText, PLAIN_AFTER, 'should convert plain text as expected')

Expand Down

0 comments on commit b3aeb8b

Please sign in to comment.