 						content: result.data
 						content_raw: result.content
 						headers: result.headers
-
-				script = undefined
-				vmScript = undefined
-				try
-					script = "result = (function() {\n"+trigger.processOutgoingResponseScript+"\n}());"
-					vmScript = vm.createScript script, 'script.js'
-					logger.outgoing.info 'will execute script processOutgoingResponseScript'
-					logger.outgoing.debug script
-					logger.outgoing.debug 'with context', sandbox
-				catch e
-					logger.outgoing.error "[Error evaluating Script:]"
-					logger.outgoing.error script.replace(/^/gm, '  ')
-					logger.outgoing.error "\n[Stack:]"
-					logger.outgoing.error e.stack.replace(/^/gm, '  ')
-					return
-
-				try
-					sandbox._ = _
-					sandbox.s = s
-					sandbox.console = console
-					vmScript.runInNewContext sandbox
-					result = sandbox.result?.content
-					if result?
-						result = data: result
-					logger.outgoing.debug 'result', result
-				catch e
-					logger.outgoing.error "[Error running Script:]"
-					logger.outgoing.error script.replace(/^/gm, '  ')
-					logger.outgoing.error "\n[Stack:]"
-					logger.outgoing.error e.stack.replace(/^/gm, '  ')
-					return
+					store: trigger.store
+
+				scriptResult = executeScript trigger.processOutgoingResponseScript, 'processOutgoingResponseScript', sandbox
+				result =
+					data: scriptResult?.content
 
 			if result?.data?.text? or result?.data?.attachments?
 				sendMessage result.data

			'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36'

	if trigger.prepareOutgoingRequestScript? and trigger.prepareOutgoingRequestScript.trim() isnt ''
		sandbox =
			request: opts

		script = undefined
		vmScript = undefined
		try
			script = "result = (function() {\n"+trigger.prepareOutgoingRequestScript+"\n}());"
			vmScript = vm.createScript script, 'script.js'
			logger.outgoing.info 'will execute script prepareOutgoingRequestScript'
			logger.outgoing.debug script
			logger.outgoing.debug 'with context', sandbox
		catch e
			logger.outgoing.error "[Error evaluating Script:]"
			logger.outgoing.error script.replace(/^/gm, '  ')
			logger.outgoing.error "\n[Stack:]"
			logger.outgoing.error e.stack.replace(/^/gm, '  ')
			return

		try
			sandbox._ = _
			sandbox.s = s
			sandbox.console = console
			vmScript.runInNewContext sandbox
			opts = sandbox.result
			logger.outgoing.debug 'result', opts
			if opts?.message?
				return sendMessage opts.message
		catch e
			logger.outgoing.error "[Error running Script:]"
			logger.outgoing.error script.replace(/^/gm, '  ')
			logger.outgoing.error "\n[Stack:]"
			logger.outgoing.error e.stack.replace(/^/gm, '  ')
			return

	if not opts?
		return


	HTTP.call opts.method, opts.url, opts, (error, result) ->
		if not result? or result.statusCode isnt 200

vm = Npm.require('vm')

triggers = {}

executeScript = (scriptContent, name, sandbox) ->
	script = undefined
	vmScript = undefined
	try
		script = "result = (function() {\n"+scriptContent+"\n}());"
		vmScript = vm.createScript script, 'script.js'
		logger.outgoing.info 'will execute script ' + name
		logger.outgoing.debug script
		logger.outgoing.debug 'with context', sandbox
	catch e
		logger.outgoing.error "[Error evaluating Script:]"
		logger.outgoing.error script.replace(/^/gm, '  ')
		logger.outgoing.error "\n[Stack:]"
		logger.outgoing.error e.stack.replace(/^/gm, '  ')
		return

	try
		sandbox._ = _
		sandbox.s = s
		sandbox.console = console

		vmScript.runInNewContext sandbox
		logger.outgoing.debug 'result', sandbox.result
		return sandbox.result
	catch e
		logger.outgoing.error "[Error running Script:]"
		logger.outgoing.error script.replace(/^/gm, '  ')
		logger.outgoing.error "\n[Stack:]"
		logger.outgoing.error e.stack.replace(/^/gm, '  ')
		return


RocketChat.models.Integrations.find({type: 'webhook-outgoing'}).observe
	added: (record) ->
