Skip to content

Commit

Permalink
feat: add types, implement confirm middleware draft
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 3, 2018
1 parent cee8ec7 commit 527c715
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 33 deletions.
11 changes: 2 additions & 9 deletions src/button.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
export interface ButtonParams {
title: string,
text: string,
tts?: string,
url?: string,
hide?: boolean,
payload?: {}
}
const button = (params: ButtonParams) => {
import { ButtonParams } from './types/button'
const button = (params: ButtonParams | string) => {
// Button has been created from string
if (typeof params === 'string') {
return {
Expand Down
42 changes: 25 additions & 17 deletions src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import ReplyBuilder from './replyBuilder'
import ButtonBuilder from './buttonBuilder'
import Command from './command'

import { WebhookResponse } from './types/webhook'

export default class Ctx {
public req: {}
public sessionId: string
Expand All @@ -22,16 +24,18 @@ export default class Ctx {
private sendResponse: (response: string) => void
private enterScene: () => void
private leaveScene: () => void
constructor({
req,
sendResponse,
session,
constructor(params) {
const {
req,
sendResponse,
session,

enterScene,
leaveScene,

enterScene,
leaveScene,
command,
} = params

command,
}) {
this.req = req
this.sendResponse = sendResponse

Expand Down Expand Up @@ -66,22 +70,26 @@ export default class Ctx {
throw new Error('Reply message could not be empty!')
}

const message = this._createReply(replyMessage)
return this._sendReply(message)
}

public _createReply(replyMessage): WebhookResponse {
/*
* Если @replyMessage — string,
* то заворачиваем в стандартную форму.
*/
* Если @replyMessage — string,
* то заворачиваем в стандартную форму.
*/
if (typeof replyMessage === 'string') {
replyMessage = this.replyBuilder
.text(replyMessage)
.tts(replyMessage)
.get()

// Is no session, lets use context session
if (!replyMessage.session) {
replyMessage.session = this.session
}
}
return this._sendReply(replyMessage)
// Is no session, lets use context session
if (!replyMessage.session) {
replyMessage.session = this.session
}
return replyMessage
}

private _sendReply(replyMessage) {
Expand Down
55 changes: 55 additions & 0 deletions src/middlewares/aliceConfirmMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

// import Ctx from '../ctx'

// alice.welcome((ctx) => {
// ctx.confirm({
// reply: '18 есть?',
// onYes: (ctx) => ctx.confirm({
// reply: 'А вы уверены?',
// onYes: (ctx) => ctx.reply('добро пожаловать'),
// }),
// onNo: (ctx) => {ctx.reply('в другой раз'),
// })
// })

// ctx.confirm(
// '18 есть',
// (ctx) => ctx.reply('good'),
// (ctx) => ctx.reply('bad'),
// )

import reply from '../reply'
// [WIP]
function aliceConfirmMiddleware() {
const store = new Map()
return (ctx) => {
Object.defineProperty(ctx, 'confirm', (
message: string,
onYesCallback: () => void,
onNoCallback: () => void,
) => {
const params = {}
if (typeof params === 'string') {
// confirmParams =

store.set(ctx.sessionId, {
onYesCallback,
onNoCallback,
})
}
const responseMessage = reply(message)
ctx.reply(responseMessage)
})
}

// const data = store.get(ctx.sessionId)
// if (data) {
// if (ctx.req.payload === data.yes) { }) {
// return data.onYes()
// } else {
// return data.onNo()
// }
// }
// return ctx
}
}
14 changes: 7 additions & 7 deletions src/tests/alice.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
const Alice = require('../alice')
const { generateRequest } = require('./testUtils')
import Alice from '../alice'
import { generateRequest } from './testUtils'

// Test for matching all command types

test('matching with string', async (done) => {
const alice = new Alice()

alice.command('Привет, как дела', (ctx) => done())
alice.handleRequestBody(generateRequest('Привет, как дела?'))
alice.handleRequest(generateRequest('Привет, как дела?'))
})

test('matching with array', async (done) => {
const alice = new Alice()

alice.command(['привет', 'как дела'], (ctx) => done())
alice.handleRequestBody(generateRequest('Привет, как дела?'))
alice.handleRequest(generateRequest('Привет, как дела?'))
})

test('matching with regexp', async (done) => {
const alice = new Alice()

alice.command(/[а-яё]+/i, (ctx) => done())
alice.handleRequestBody(generateRequest('Привет как дела'))
alice.handleRequest(generateRequest('Привет как дела'))
})

test('priority check, strings over regexps', async (done) => {
const alice = new Alice()

alice.command(/[а-яё]+/i, (ctx) => new Error('Error has occured'))
alice.command('привет', (ctx) => done())
alice.handleRequestBody(generateRequest('Привет как дела'))
alice.handleRequest(generateRequest('Привет как дела'))
})

test('listenining on port with callback', async (done) => {
Expand Down Expand Up @@ -61,7 +61,7 @@ test('ctx body', async (done) => {
})
done()
})
alice.handleRequestBody(
alice.handleRequest(
generateRequest('забронируй встречу в 7-холмов на 18:00'),
)
})
16 changes: 16 additions & 0 deletions src/types/button.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface ButtonParams {
title?: string,
text?: string,
tts?: string,
url?: string,
hide?: boolean,
payload?: {}
}

export interface Button {
title: string,
tts?:
url:
hide: boolean
payload?: {}
}

0 comments on commit 527c715

Please sign in to comment.