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

feat: list robot joined rooms #24

Merged
merged 2 commits into from
Dec 21, 2019
Merged
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
35 changes: 21 additions & 14 deletions src/start-web.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Hapi, { Request, ResponseToolkit } from '@hapi/hapi'
import Hapi, { Request, ResponseToolkit } from '@hapi/hapi'
import {
Wechaty,
} from 'wechaty'
} from 'wechaty'

import {
log,
PORT,
VERSION,
} from './config'
} from './config'
import { chatops } from './chatops'

let wechaty: Wechaty
Expand All @@ -27,12 +27,12 @@ async function chatopsHandler (request: Request, response: ResponseToolkit) {
export async function startWeb (bot: Wechaty): Promise<void> {
log.verbose('startWeb', 'startWeb(%s)', bot)

let qrcodeValue : undefined | string
let userName : undefined | string
let qrcodeValue: undefined | string
let userName: undefined | string

wechaty = bot

const server = new Hapi.Server({
const server = new Hapi.Server({
port: PORT,
})

Expand All @@ -43,7 +43,7 @@ export async function startWeb (bot: Wechaty): Promise<void> {
<input type="submit" value="ChatOps">
</form>
`
const handler = () => {
const handler = async () => {
let html

if (qrcodeValue) {
Expand All @@ -61,12 +61,19 @@ export async function startWeb (bot: Wechaty): Promise<void> {
].join('')

} else if (userName) {
let rooms = await bot.Room.findAll()
let roomHtml = `The rooms I have joined are as follows: <ol>`
for (let room of rooms) {
const topic = await room.topic()
roomHtml = roomHtml + `<li>` + topic + `</li>\n`
}
roomHtml = roomHtml + `</ol>`

html = [
`<p> OSS Bot v${VERSION} User ${userName} logined. </p>`,
FORM_HTML,
roomHtml,
].join('')

} else {

html = `OSS Bot v${VERSION} Hello, come back later please.`
Expand All @@ -77,23 +84,23 @@ export async function startWeb (bot: Wechaty): Promise<void> {

server.route({
handler,
method : 'GET',
path : '/',
method: 'GET',
path: '/',
})

server.route({
handler: chatopsHandler,
method : 'POST',
path : '/chatops/',
method: 'POST',
path: '/chatops/',
})

bot.on('scan', qrcode => {
qrcodeValue = qrcode
userName = undefined
userName = undefined
})
bot.on('login', user => {
qrcodeValue = undefined
userName = user.name()
userName = user.name()
})
bot.on('logout', () => {
userName = undefined
Expand Down