1717var env = process . env ;
1818var util = require ( 'util' ) ;
1919var utils = require ( './../utils' ) ;
20+ var messages = require ( './../slack-messages' ) ;
2021var SlackLikeAdapter = require ( './slack-like' ) ;
2122
2223
@@ -29,15 +30,21 @@ util.inherits(MattermostAdapter, SlackLikeAdapter);
2930
3031MattermostAdapter . prototype . postData = function ( data ) {
3132 var self = this ;
32-
33- var recipient , attachment_color , split_message ,
33+ var attachment_color , split_message , envelope ,
3434 attachment , pretext = "" ;
35+ // We capture robot here so the `sendChunk` closure captures the correct
36+ // `this`
37+ var robot = self . robot ;
3538
3639 // If we are supposed to whisper to a single user, use a direct message
3740 if ( data . whisper && data . user ) {
38- recipient = data . user ;
41+ envelope = {
42+ room : data . user
43+ } ;
3944 } else { // Otherwise, message the channel
40- recipient = data . channel ;
45+ envelope = {
46+ room : data . channel
47+ } ;
4148 // If we aren't supposed to whisper, then we at least at-mention the user
4249 pretext = ( data . user && ! data . whisper ) ? util . format ( '@%s: ' , data . user ) : "" ;
4350 }
@@ -56,7 +63,6 @@ MattermostAdapter.prototype.postData = function(data) {
5663 }
5764
5865 split_message = utils . splitMessage ( self . formatData ( data . message ) ) ;
59-
6066 if ( split_message . text ) {
6167 // Default values
6268 var content = {
@@ -65,12 +71,57 @@ MattermostAdapter.prototype.postData = function(data) {
6571 } ;
6672 // Override the default values with values from `data.extra.mattermost`
6773 if ( data . extra && data . extra . mattermost ) {
74+ // Backwards compatibility
75+
76+ // Action:
77+ //
78+ // result:
79+ // format: ...
80+ // message: Message text
81+ // extra:
82+ // mattermost:
83+ // author_name: Jira_Bot
84+ // author_link: "https://stackstorm.com"
85+ // author_icon: "https://stackstorm.com/favicon.ico"
86+ // color: "#042A60"
87+ // fallback: "Info about Jira ticket {{ execution.result.result.key }}"
88+ // title: "{{ execution.result.result.key }}"
89+ // title_link: "{{ execution.result.result.url }}"
90+ // fields:
91+ // -
92+ // title: Summary
93+ // value: "{{ execution.result.result.summary }}"
94+ // short: false
95+ //
96+ // becomes:
97+ //
98+ // {
99+ // "message": "Message text",
100+ // "props": {
101+ // "attachments": [
102+ // {
103+ // "author_name": "Jira Bot",
104+ // "author_link": "https://stackstorm.com",
105+ // "author_icon": "https://stackstorm.com/favicon.ico",
106+ // "color": "#042A60",
107+ // "fallback": "Info about Jira ticket {{ execution.result.result.key }}",
108+ // "title": "{{ execution.result.result.key }}",
109+ // "title_link": "{{ execution.result.result.url }}",
110+ // "fields": [
111+ // {
112+ // "title": "Summary",
113+ // "value": "{{ execution.result.result.summary }}",
114+ // "short": false
115+ // }
116+ // ]
117+ // }
118+ // ]
119+ // }
120+ // }
121+
68122 for ( var attrname in data . extra . mattermost ) { content [ attrname ] = data . extra . mattermost [ attrname ] ; }
69123 }
70124
71- // We capture robot here so the `sendMessage` closure captures the correct
72- // `this`
73- var robot = self . robot ;
74125 var chunks = split_message . text . match ( / [ \s \S ] { 1 , 3800 } / g) ;
75126
76127 // We define a recursive closure that calls itself with the next data to
@@ -85,23 +136,25 @@ MattermostAdapter.prototype.postData = function(data) {
85136 Inorder to accept the matteruser adapter message attachments changed the attachement json
86137 */
87138
88- attachment = {
89- room : recipient ,
90- attachments : content . attachments ? content . attachments : content ,
139+ var message = {
140+ props : {
141+ attachments : ( content . attachments ? content . attachments : [ content ] )
142+ } ,
91143 // There is likely a bug here - `split_message.text` being a true-y
92144 // value does not imply that `split_message.pretext` is also non-empty,
93145 // but we unconditionally set `text` to
94146 // `pretext + split_message.pretext` on the first message
95- text : i === 0 ? pretext + split_message . pretext : null
147+ message : i === 0 ? pretext + split_message . pretext : null
96148 } ;
97- robot . emit ( 'slack-attachment' , attachment ) ;
149+
150+ robot . adapter . send ( envelope , message ) ;
98151 if ( chunks . length > ++ i ) {
99152 setTimeout ( function ( ) { sendChunk ( i ) ; } , 300 ) ;
100153 }
101154 } ;
102155 sendChunk ( 0 ) ;
103156 } else {
104- self . robot . messageRoom . call ( self . robot , recipient , pretext + split_message . pretext ) ;
157+ robot . messageRoom . call ( self . robot , envelope . room , pretext + split_message . pretext ) ;
105158 }
106159} ;
107160
0 commit comments