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

Room bot example #1325

Merged
merged 3 commits into from
Jun 13, 2018
Merged
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
13 changes: 10 additions & 3 deletions examples/room-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,35 +121,41 @@ bot
/**
* 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',
await room.topic(),
inviteeList.map(c => c.name()).join(','),
inviter.name(),
)
const topic = await room.topic()
room.say(`welcome to ${topic}!`, inviteeList[0])
})

/**
* 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',
await room.topic(),
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
*/
.on('room-topic', function(this, room, topic, oldTopic, changer) {
.on('room-topic', function(room, topic, oldTopic, changer) {
try {
log.info('Bot', 'EVENT: room-topic - Room %s change topic from %s to %s by member %s',
room,
oldTopic,
topic,
changer,
)
room.say(`room-topic - change topic from ${oldTopic} to ${topic} by member ${changer.name()}` )
} catch (e) {
log.error('Bot', 'room-topic event exception: %s', e.stack)
}
Expand Down Expand Up @@ -380,6 +386,7 @@ async function createDingRoom(contact: Contact): Promise<any> {

if (!helperContact) {
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
}

Expand Down
2 changes: 1 addition & 1 deletion src/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export class Room extends Accessory implements Sayable {
}
await this.puppet.messageSendText({
roomId: this.id,
contactId: replyToList[0].id,
contactId: (replyToList && replyToList.length && replyToList[0].id) || undefined,
}, text)
} else if (textOrContactOrFile instanceof FileBox) {
await this.puppet.messageSendFile({
Expand Down