From 56802e2c049cae7009d11315ccf32964e11d41c1 Mon Sep 17 00:00:00 2001 From: Sundeep Gupta Date: Wed, 12 Oct 2016 23:34:25 -0400 Subject: [PATCH] Slack's `chat.update` should behave the same as `chat.postMessage` When attachments are sent to these API methods, they need to be handled in the same way. `postMessage` was already doing it, so had `update` do the same. --- lib/Slack_web_api.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/Slack_web_api.js b/lib/Slack_web_api.js index 2e46e3760..7724dda6b 100755 --- a/lib/Slack_web_api.js +++ b/lib/Slack_web_api.js @@ -161,17 +161,27 @@ module.exports = function(bot, config) { // overwrite default behavior slack_api.chat.postMessage = function(options, cb) { - if (options.attachments && typeof(options.attachments) != 'string') { - try { - options.attachments = JSON.stringify(options.attachments); - } catch (err) { - delete options.attachments; - bot.log.error('Could not parse attachments', err); - } - } + sanitizeOptions(options); slack_api.callAPI('chat.postMessage', options, cb); }; + slack_api.chat.update = function(options, cb) { + sanitizeOptions(options); + slack_api.callAPI('chat.update', options, cb); + }; + + function sanitizeOptions(options) { + if (options.attachments && typeof(options.attachments) != 'string') { + try { + options.attachments = JSON.stringify(options.attachments); + } catch (err) { + delete options.attachments; + bot.log.error('Could not parse attachments', err); + } + } + } + + return slack_api;