未验证 提交 1909ec92 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓) 提交者: GitHub

Merge pull request #1325 from lijiarui/room-bot-example

Room bot example
...@@ -121,28 +121,33 @@ bot ...@@ -121,28 +121,33 @@ bot
/** /**
* Global Event: room-join * Global Event: room-join
*/ */
.on('room-join', async function(this, room, inviteeList, inviter) { .on('room-join', async function(room, inviteeList, inviter) {
log.info( 'Bot', 'EVENT: room-join - Room %s got new member %s, invited by %s', log.info( 'Bot', 'EVENT: room-join - Room %s got new member %s, invited by %s',
await room.topic(), await room.topic(),
inviteeList.map(c => c.name()).join(','), inviteeList.map(c => c.name()).join(','),
inviter.name(), inviter.name(),
) )
const topic = await room.topic()
room.say(`welcome to ${topic}!`, inviteeList[0])
}) })
/** /**
* Global Event: room-leave * Global Event: room-leave
*/ */
.on('room-leave', async function(this, room, leaverList) { .on('room-leave', async function(room, leaverList) {
log.info('Bot', 'EVENT: room-leave - Room %s lost member %s', log.info('Bot', 'EVENT: room-leave - Room %s lost member %s',
await room.topic(), await room.topic(),
leaverList.map(c => c.name()).join(','), leaverList.map(c => c.name()).join(','),
) )
const topic = await room.topic()
const name = leaverList[0] ? leaverList[0].name() : 'no contact!'
room.say(`kick off ${name} from ${topic}!` )
}) })
/** /**
* Global Event: room-topic * Global Event: room-topic
*/ */
.on('room-topic', function(this, room, topic, oldTopic, changer) { .on('room-topic', function(room, topic, oldTopic, changer) {
try { try {
log.info('Bot', 'EVENT: room-topic - Room %s change topic from %s to %s by member %s', log.info('Bot', 'EVENT: room-topic - Room %s change topic from %s to %s by member %s',
room, room,
...@@ -150,6 +155,7 @@ bot ...@@ -150,6 +155,7 @@ bot
topic, topic,
changer, changer,
) )
room.say(`room-topic - change topic from ${oldTopic} to ${topic} by member ${changer.name()}` )
} catch (e) { } catch (e) {
log.error('Bot', 'room-topic event exception: %s', e.stack) log.error('Bot', 'room-topic event exception: %s', e.stack)
} }
...@@ -380,6 +386,7 @@ async function createDingRoom(contact: Contact): Promise<any> { ...@@ -380,6 +386,7 @@ async function createDingRoom(contact: Contact): Promise<any> {
if (!helperContact) { if (!helperContact) {
log.warn('Bot', 'getHelperContact() found nobody') log.warn('Bot', 'getHelperContact() found nobody')
contact.say(`You don't have a friend called ${HELPER_CONTACT_NAME}, because create a new room at least need 3 contacts, please set [HELPER_CONTACT_NAME] in the code first!`)
return return
} }
......
...@@ -318,7 +318,7 @@ export class Room extends Accessory implements Sayable { ...@@ -318,7 +318,7 @@ export class Room extends Accessory implements Sayable {
} }
await this.puppet.messageSendText({ await this.puppet.messageSendText({
roomId: this.id, roomId: this.id,
contactId: replyToList[0].id, contactId: (replyToList && replyToList.length && replyToList[0].id) || undefined,
}, text) }, text)
} else if (textOrContactOrFile instanceof FileBox) { } else if (textOrContactOrFile instanceof FileBox) {
await this.puppet.messageSendFile({ await this.puppet.messageSendFile({
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册