import { ActivityTypes , Middleware , TurnContext } from 'botbuilder' ; import * as WebRequest from 'web-request' ; module function getUrl ( text ) { return `https://api.cognitive.microsoft.com/bing/v7.0/spellcheck/?text=${ text }&mode=spell` ; } async function getWebRequest ( url , key ) { return await WebRequest . get ( url , { headers : { 'Content-Type' : 'application/x-www-form-urlencoded' , 'Ocp-Apim-Subscription-Key' : key } } ) ; } export class SpellCheck implements Middleware { public text : string ; public key : string ; public constructor ( key ) { this . key = key ; } public async onTurn ( context , next ) { if ( context . activity . type === ActivityTypes . Message ) { this . text = context . activity . text ; const url = getUrl ( this . text ) ; try { const re = await getWebRequest ( url , this . key ) ; const obj = JSON . parse ( re . content ) ; if ( obj . flaggedTokens && obj . flaggedTokens . length > 0 ) { try { if ( obj . flaggedTokens [ 0 ] . suggestions [ 0 ] . suggestion ) { const suggestion = obj . flaggedTokens [ 0 ] . suggestions [ 0 ] . suggestion ; const token = obj . flaggedTokens [ 0 ] . token ; context . turnState . set ( 'token' , token ) ; context . turnState . set ( 'suggestion' , suggestion ) ; } } catch ( error ) { throw new Error ( error ) ; } } } catch ( e ) { throw new Error ( `Failed to process spellcheck on ${ context . activity . text }. Error: ${ e }` ) ; } } await next ( ) ; } }