import * as path from 'path' ; import { config } from 'dotenv' ; const ENV_FILE = path . join ( __dirname , '..' , '.env' ) ; const settings = config ( { path : ENV_FILE } ) ; import * as restify from 'restify' ; import * as request from 'request' ; import * as express from 'express' ; import * as corsMiddleware from 'restify-cors-middleware' ; import { BotFrameworkAdapter } from 'botbuilder' ; import { EchoBot } from './bot' ; import { Guid } from './util' ; const cors = corsMiddleware ( { origins : [ '*' ] , allowHeaders : [ '*' ] , exposeHeaders : [ '*' ] } ) ; const server = restify . createServer ( ) ; server . pre ( cors . preflight ) ; server . use ( cors . actual ) ; server . use ( express . json ( ) ) ; server . listen ( process . env . port || process . env . PORT || 3978 , ( ) => { console . log ( `\n${ server . name } listening to ${ server . url }` ) ; console . log ( '\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator' ) ; console . log ( '\nTo talk to your bot, open the emulator select "Open Bot"' ) ; } ) ; const adapter = new BotFrameworkAdapter ( { appId : process . env . MicrosoftAppId , appPassword : process . env . MicrosoftAppPassword } ) ; const onTurnErrorHandler = async ( context , error ) => { console . error ( `\n [onTurnError] unhandled error: ${ error }` ) ; await context . sendTraceActivity ( 'OnTurnError Trace' , `${ error }` , 'https://www.botframework.com/schemas/error' , 'TurnError' ) ; await context . sendActivity ( 'The bot encountered an error or bug.' ) ; await context . sendActivity ( 'To continue to run this bot, please fix the bot source code.' ) ; } ; adapter . onTurnError = onTurnErrorHandler ; const myBot = new EchoBot ( ) ; server . post ( '/api/messages' , ( req , res ) => { adapter . processActivity ( req , res , async ( context ) => { await myBot . run ( context ) ; } ) ; } ) ; const GetUserId = ( userName ) : string => { const userId = userName ? userName : Guid . newGuid ( ) ; return userId ; } ; server . post ( '/directline/token' , ( req , res ) => { const secret = settings . parsed . DirectLineSecret ; const authorization = `Bearer ${ secret }` ; const userId = 'dl_' + GetUserId ( ( req . body || { } ) . user ) ; const options = { method : 'POST' , uri : 'https://directline.botframework.com/v3/directline/tokens/generate' , body : JSON . stringify ( { user : { id : userId } } ) , headers : { 'Authorization' : authorization , 'Content-Type' : 'application/json' } } ; request . post ( options , ( error , response , body ) => { if ( ! error && response . statusCode < 300 ) { res . status ( response . statusCode ) ; if ( body ) { res . send ( JSON . parse ( body ) ) ; } } else { res . status ( 500 ) ; res . send ( 'Call to retrieve token from DirectLine failed' ) ; } res . end ( ) ; } ) ; } ) ; server . on ( 'upgrade' , ( req , socket , head ) => { const streamingAdapter = new BotFrameworkAdapter ( { appId : process . env . MicrosoftAppId , appPassword : process . env . MicrosoftAppPassword } ) ; streamingAdapter . onTurnError = onTurnErrorHandler ; streamingAdapter . useWebSocket ( req , socket , head , async ( context ) => { await myBot . run ( context ) ; } ) ; } ) ;