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

wip #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
76 changes: 68 additions & 8 deletions src/proactive/install.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Db } from 'mongodb';
import { User } from './../users/model/User';
import { UniversalBot } from 'botbuilder';
import { UniversalBot, Message, IMessage } from 'botbuilder';
import { Application } from 'express'
import * as moment from 'moment'
import * as async from 'async'

export type ProactiveHandler = (bot: UniversalBot, user: User, log: Logger, next, args: any) => void
export type ProactiveHandler = (bot: UniversalBot, user: User, logger: ProactiveLogger, next, args: any) => void

export interface ILogEntry {
data: any,
timestamp?: number
}

export class Logger {
export class ProactiveLogger {

constructor(private id: string, private db: Db) {
}
Expand All @@ -37,23 +37,29 @@ export class Logger {

}

const handlers: { [id: string]: { logger: Logger, query: any, callback: ProactiveHandler } } = {}

const handlers: { [id: string]: { logger: ProactiveLogger, query: any, callback: ProactiveHandler } } = {}
let database: Db;

export interface IProactivHandlerConfig {
id: string,
db: Db,
query?: any,
handler: ProactiveHandler
}

export function add(config:IProactivHandlerConfig) {
export function add(config: IProactivHandlerConfig) {

handlers[config.id] = { logger: new ProactiveLogger(config.id, database), query: config.query || {}, callback: config.handler }

handlers[config.id] = { logger: new Logger(config.id, config.db), query: config.query || {}, callback: config.handler }
console.log('Added proactive handler in: /api/proactive/', config.id)
}

export function install(bot: UniversalBot, db: Db, server: Application) {

//module wide instance
database = db;

//setup api endpoint

server.post('/api/proactive/:id', (req, res, next) => {

const id = req.params.id;
Expand All @@ -70,6 +76,8 @@ export function install(bot: UniversalBot, db: Db, server: Application) {

handlers[id].callback(bot, user, handlers[id].logger, next, req.params);
});

return users;
});
}
else {
Expand All @@ -78,4 +86,56 @@ export function install(bot: UniversalBot, db: Db, server: Application) {

next()
})

add
({
id: 'sendmessage',
query: {},
handler: (bot: UniversalBot, user: User, logger: ProactiveLogger, next, args: any) => {

let address = user.addresses[args.channelId]

let message = new Message()
.text(args.text)
.address(address)

bot.send(message, (err) => {

if (err) {
console.error('ERROR begining dialog to', address.user.name)
}
else {


console.info('Succesfully begun dialog to', address.user.name)
}

logger.log({data: {errror: err ? err : null}})

next()
})
}
})

add
({
id: 'begindialog',
query: {},
handler: (bot: UniversalBot, user: User, logger: ProactiveLogger, next, args: any) => {

let address = user.addresses[args.channelId]

bot.beginDialog(address, args.dialogId, args.dialogArgs, (err) => {

if (err) {
console.error('ERROR begining dialog to', address.user.name)
}
else {
console.info('Succesfully begun dialog to', address.user.name)
}
next()
})
}
})

}