import * as _ from "underscore" ; import { Looker } from "./looker" ; import { Message , SentMessage } from "./message" ; export class ReplyContext { public looker ! : Looker ; public sourceMessage : SentMessage ; public isDM = false ; public scheduled = false ; public dataAction = false ; private hasRepliedPrivately = false ; constructor ( private defaultBot , private messageBot , sourceMessage ) { this . defaultBot = defaultBot ; this . messageBot = messageBot ; this . sourceMessage = sourceMessage ; } public isSlashCommand ( ) { return ! ! this . messageBot . res ; } public replyPrivate ( message , cb ? ) { message = this . rtmOptOut ( message ) ; if ( this . isSlashCommand ( ) ) { if ( this . hasRepliedPrivately ) { return this . messageBot . replyPrivateDelayed ( this . sourceMessage , message , cb ) ; } else { this . hasRepliedPrivately = true ; return this . messageBot . replyPrivate ( this . sourceMessage , message , cb ) ; } } else { return this . defaultBot . reply ( this . sourceMessage , message , cb ) ; } } public replyPublic ( message , cb ? ) { message = this . rtmOptOut ( message ) ; if ( this . isSlashCommand ( ) ) { return this . messageBot . replyPublicDelayed ( this . sourceMessage , message , cb ) ; } else { return this . defaultBot . reply ( this . sourceMessage , message , cb ) ; } } public startTyping ( ) { if ( this . isSlashCommand ( ) ) { return this . messageBot . replyPublicDelayed ( this . sourceMessage , { type : "typing" } ) ; } else { return this . defaultBot . startTyping ( this . sourceMessage ) ; } } public completeSlashCommand ( ) { if ( this . isSlashCommand ( ) && ! this . hasRepliedPrivately ) { this . messageBot . res . setHeader ( "Content-Type" , "application/json" ) ; this . messageBot . res . send ( JSON . stringify ( { response_type : "in_channel" } ) ) ; } } public updateMessage ( updatedMessage ) { this . defaultBot . api . chat . update ( updatedMessage ) ; } private rtmOptOut ( message ) { if ( typeof ( message ) === "string" ) { return { text : message , attachments : [ ] } ; } else { if ( message . attachments == null ) { message . attachments = [ ] ; } return message ; } } }