提交 3ba33137 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

#52 support `room-*` event for chinese wechat

上级 1d7d89be
......@@ -84,7 +84,7 @@
"dependencies": {
"arrify": "^1.0.1",
"body-parser": "^1.15.2",
"brolog": "^0.3.7",
"brolog": "^0.3.10",
"chromedriver": "^2.23.1",
"express": "^4.13.4",
"is-ci": "^1.0.9",
......
......@@ -12,11 +12,7 @@
*/
import { test } from 'ava'
// import Contact from '../contact'
// import Message from '../message'
// import FriendRequest from './friend-request'
import Firer from './firer'
import Firer from './firer'
test('Firer smoking test', t => {
......@@ -56,6 +52,11 @@ test('Firer.checkRoomJoin', t => {
, `凌`
, ['庆次', '小桔妹']
]
, [
`你邀请"李佳芮"加入了群聊`
, ''
, ['李佳芮']
]
]
let result
......@@ -72,15 +73,23 @@ test('Firer.checkRoomJoin', t => {
})
test('Firer.checkRoomLeave', t => {
const data = [
`You removed "Bruce LEE" from the group chat`
, `Bruce LEE`
const contentList = [
[
`You removed "Bruce LEE" from the group chat`
, `Bruce LEE`
]
, [
'你将"李佳芮"移出了群聊'
, '李佳芮'
]
]
let leaver
leaver = Firer.checkRoomLeave(data[0])
t.truthy(leaver, 'should get leaver for leave message')
t.is(leaver, data[1], 'should get leaver name right')
let result
contentList.forEach(([content, leaver]) => {
result = Firer.checkRoomLeave(content)
t.truthy(result, 'should get leaver for leave message: ' + content)
t.is(result, leaver, 'should get leaver name right')
})
t.throws(() => {
Firer.checkRoomLeave('fafdsfsdfafa')
......@@ -88,16 +97,29 @@ test('Firer.checkRoomLeave', t => {
})
test('Firer.checkRoomTopic', t => {
const data = [
`"李卓桓.PreAngel" changed the group name to "ding"`
, `李卓桓.PreAngel`
, `ding`
const contentList = [
[
`"李卓桓.PreAngel" changed the group name to "ding"`
, `李卓桓.PreAngel`
, `ding`
]
, [
'"李佳芮"修改群名为“dong”'
, '李佳芮'
, 'dong'
]
]
const result = Firer.checkRoomTopic(data[0])
t.truthy(result, 'should check topic right')
let result
contentList.forEach(([content, changer, topic]) => {
result = Firer.checkRoomTopic(content)
t.truthy(result, 'should check topic right for content: ' + content)
t.is(topic , result[2], 'should get right topic')
t.is(changer, result[1], 'should get right changer')
})
t.throws(() => {
Firer.checkRoomTopic('fafdsfsdfafa')
}, 'should throw if message is not expected')
const [topic, changer] = result as [string, string]
t.is(topic , data[2], 'should get right topic')
t.is(changer, data[1], 'should get right changer')
})
......@@ -50,11 +50,22 @@ const PuppetWebFirer = {
}
const regexConfig = {
friendConfirm: /^You have added (.+) as your WeChat contact. Start chatting!$/
, roomJoin: /^"?(.+?)"? invited "(.+)" to the group chat$/
, roomLeave: /^You removed "(.+)" from the group chat$/
, roomTopic: /^"?(.+?)"? changed the group name to "(.+)"$/
friendConfirm: [
/^You have added (.+) as your WeChat contact. Start chatting!$/
]
, roomJoin: [
/^"?(.+?)"? invited "(.+)" to the group chat$/
, /^"?(.+?)"?邀请"(.+)"加入了群聊$/
]
, roomLeave: [
/^You removed "(.+)" from the group chat$/
, /^你将"(.+)"移出了群聊$/
]
, roomTopic: [
/^"?(.+?)"? changed the group name to "(.+)"$/
, /^"?(.+?)"?修改群名为“(.+)”$/
]
}
async function fireFriendRequest(m: Message) {
......@@ -82,8 +93,10 @@ async function fireFriendRequest(m: Message) {
* try to find FriendRequest Confirmation Message
*/
function checkFriendConfirm(content) {
const re = regexConfig.friendConfirm
if (re.test(content)) {
const reList = regexConfig.friendConfirm
let found = false
reList.some(re => !!(found = re.test(content)))
if (found) {
return true
} else {
return false
......@@ -121,10 +134,11 @@ async function fireFriendConfirm(m: Message) {
function checkRoomJoin(content: string): [string[], string] {
log.verbose('PuppetWebFirer', 'checkRoomJoin()')
const re = regexConfig.roomJoin
const reList = regexConfig.roomJoin
const found = content.match(re)
if (!found) {
let found: string[]|null = []
reList.some(re => !!(found = content.match(re)))
if (!found || !found.length) {
throw new Error('checkRoomJoin() not found')
}
......@@ -249,10 +263,11 @@ async function fireRoomJoin(m: Message): Promise<void> {
}
function checkRoomLeave(content: string): string|null {
const re = regexConfig.roomLeave
const reList = regexConfig.roomLeave
const found = content.match(re)
if (!found) {
let found: string[]|null = []
reList.some(re => !!(found = content.match(re)))
if (!found || !found.length) {
return null
}
return found[1] // leaver
......@@ -290,10 +305,11 @@ async function fireRoomLeave(m: Message) {
}
function checkRoomTopic(content: string): [string, string] {
const re = regexConfig.roomTopic
const reList = regexConfig.roomTopic
const found = content.match(re)
if (!found) {
let found: string[]|null = []
reList.some(re => !!(found = content.match(re)))
if (!found || !found.length) {
throw new Error('checkRoomTopic() not found')
}
const [, changer, topic] = found
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册