import http from 'http' ; import querystring from 'querystring' ; import requestLib from 'request' ; import { Cli , Bridge , AppServiceRegistration } from 'matrix-appservice-bridge' ; const PORT = 9898 ; const ROOM_ID = "!YiuxjYhPLIZGVVkFjT:localhost" ; const SLACK_WEBHOOK_URL = "https://hooks.slack.com/services/AAAA/BBBBB/CCCCC" ; let bridge ; http . createServer ( function ( request , response ) { console . log ( request . method + " " + request . url ) ; let body = '' ; request . on ( 'data' , function ( chunk ) { body += chunk ; } ) ; request . on ( "end" , function ( ) { const params = querystring . parse ( body ) ; if ( params . user_id !== "USLACKBOT" ) { const intent = bridge . getIntent ( `@slack_${ params . user_name }:localhost` ) ; intent . sendText ( ROOM_ID , Array . isArray ( params . text ) ? params . text . join ( ' ' ) : params . text ) ; } response . writeHead ( 200 , { "Content-Type" : "application/json" } ) ; response . write ( JSON . stringify ( { } ) ) ; response . end ( ) ; } ) ; } ) . listen ( PORT ) ; new Cli ( { registrationPath : "slack-registration.yaml" , generateRegistration : function ( reg , callback ) { reg . setId ( AppServiceRegistration . generateToken ( ) ) ; reg . setHomeserverToken ( AppServiceRegistration . generateToken ( ) ) ; reg . setAppServiceToken ( AppServiceRegistration . generateToken ( ) ) ; reg . setSenderLocalpart ( "slackbot" ) ; reg . addRegexPattern ( "users" , "@slack_.*" , true ) ; callback ( reg ) ; } , run : function ( port ) { bridge = new Bridge ( { homeserverUrl : "http://localhost:8008" , domain : "localhost" , registration : "slack-registration.yaml" , controller : { onUserQuery : function ( queriedUser ) { return { } ; } , onEvent : function ( request , context ) { const event = request . getData ( ) ; if ( event . type !== "m.room.message" || ! event . content || event . room_id !== ROOM_ID ) { return ; } requestLib ( { method : "POST" , json : true , uri : SLACK_WEBHOOK_URL , body : { username : event . sender , text : event . content . body } } , function ( err , res ) { if ( err ) { console . log ( "HTTP Error: %s" , err ) ; } else { console . log ( "HTTP %s" , res . statusCode ) ; } } ) ; } } } ) ; console . log ( "Matrix-side listening on port %s" , port ) ; bridge . run ( port ) ; } } ) . run ( ) ;