import { Wit } from 'node-wit' ; import * as _ from 'lodash' ; import * as emoji from 'node-emoji' ; import { Intent } from 'src/common/interfaces' ; import { wit as witConfig , comments as commentsConfig } from 'src/config' ; import { getRandomItem } from 'src/common/utils' ; const client = new Wit ( _ . pick ( witConfig , [ 'accessToken' ] ) ) ; const getIntentFromMessage = async ( message , context = { } ) : Promise < Intent > => { if ( ! message || message . length > 280 ) { return { confidence : 0 , value : '' , } ; } const result = await client . message ( message , context ) ; return _ . get ( result , [ 'entities' , 'intent' , '0' ] ) ; } ; const intentToCategory = { happy_description : 'happy' , sad_description : 'sad' , } ; const emojis = { happy : [ 'smiley' , 'smirk' , 'pray' , 'rocket' , 'kissing_closed_eyes' , 'sunglasses' , 'heart_eyes' , 'joy' , 'relieved' , 'wink' , 'innocent' , 'smiling_imp' , 'sweat_smile' , 'blush' , 'yum' , 'triumph' , ] , sad : [ 'cry' , 'sob' , 'cold_sweat' , 'disappointed' , 'worried' ] , } ; const getRandomEmoji = ( type ) => { const emojisForType = emojis [ type ] ; if ( ! emojisForType ) { return '' ; } const emojiName = getRandomItem < string > ( emojisForType ) ; return emoji . get ( emojiName ) ; } ; const getRandomEmojis = ( count , type ) => new Array ( count ) . fill ( type ) . map ( getRandomEmoji ) . join ( '' ) ; const shouldPostComment = ( intent ) : boolean => intent . confidence >= witConfig . expectedConfidence ; const generateComment = ( category ) : string => { const randomEmojis = '' ; return [ getRandomItem < string > ( commentsConfig [ category ] ) , randomEmojis ] . join ( ' ' ) ; } ; const getMessageBasedOnIntent = ( intent ) : string => { const category = intentToCategory [ intent . value ] ; if ( ! category || ! shouldPostComment ( intent ) ) { return '' ; } return generateComment ( category ) ; } ; export { client , getIntentFromMessage , getMessageBasedOnIntent , shouldPostComment } ;