{"action":"created","issue":{"url":"https://api.github.com/repos/samuelcouch/slack-attack/issues/2","labels_url":"https://api.github.com/repos/samuelcouch/slack-attack/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/samuelcouch/slack-attack/issues/2/comments","events_url":"https://api.github.com/repos/samuelcouch/slack-attack/issues/2/events","html_url":"https://github.com/samuelcouch/slack-attack/issues/2","id":117247130,"number":2,"title":"Specifying attachments removes username and other top-level options","user":{"login":"thecubed","id":846632,"avatar_url":"https://avatars.githubusercontent.com/u/846632?v=3","gravatar_id":"","url":"https://api.github.com/users/thecubed","html_url":"https://github.com/thecubed","followers_url":"https://api.github.com/users/thecubed/followers","following_url":"https://api.github.com/users/thecubed/following{/other_user}","gists_url":"https://api.github.com/users/thecubed/gists{/gist_id}","starred_url":"https://api.github.com/users/thecubed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thecubed/subscriptions","organizations_url":"https://api.github.com/users/thecubed/orgs","repos_url":"https://api.github.com/users/thecubed/repos","events_url":"https://api.github.com/users/thecubed/events{/privacy}","received_events_url":"https://api.github.com/users/thecubed/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-11-16T23:54:12Z","updated_at":"2015-11-17T16:56:07Z","closed_at":null,"body":"It would seem that specifying attachments in your 'options' object causes the top-level options to be erased (such as 'username').\r\n\r\n```\r\nvar options = {\r\n\t\"username\": \"MrPotatoHead\",\r\n\t\"icon_emoji\": \":robot_face:\",\r\n\t\"link_names\": 1,\r\n\t\"attachments\": [\r\n\t\t{\r\n\t\t\t\"fallback\": \"Message content only available on Slack client.\",\r\n\t\t\t\"color\": \"error\",\r\n\t\t\t\"title\": \"Test Results\",\r\n\t\t\t\"text\": \"There was an error with the test.\",\r\n\t\t\t\"thumb_url\": \"http://google.com/logo.png\",\r\n\t\t\t\"fields\": [\r\n\t\t\t\t{\r\n\t\t\t\t\t\"title\": \"Test URL\",\r\n\t\t\t\t\t\"value\": \"http://google.com\",\r\n\t\t\t\t\t\"short\": false\r\n\t\t\t\t}        \r\n\t\t\t]\r\n\r\n\t\t}\r\n\t]\r\n}\r\nslack.chat('#mychannel', 'Test', options, function(err, posted, resp){\r\n      console.log(err, posted, resp);\r\n});\r\n```\r\n\r\nThe 'options' object ends up missing the 'username', 'icon_emoji', and any other top-level options I specify. \r\n\r\nThis causes the message to come as \"bot\" with no name.\r\n\r\nIf I instead do:\r\n```\r\nvar options = {\r\n\t\"username\": \"MrPotatoHead\",\r\n\t\"icon_emoji\": \":robot_face:\",\r\n\t\"link_names\": 1,\r\n\t\"channel\": \"#mychannel\",\r\n\t\"attachments\": JSON.stringify([\r\n\t\t{\r\n\t\t\t\"fallback\": \"Message content only available on Slack client.\",\r\n\t\t\t\"color\": \"error\",\r\n\t\t\t\"title\": \"Test Results\",\r\n\t\t\t\"text\": \"There was an error with the test.\",\r\n\r\n\t\t\t\"thumb_url\": \"http://google.com/logo.png\",\r\n\t\t\t\"fields\": [\r\n\t\t\t\t{\r\n\t\t\t\t\t\"title\": \"Test URL\",\r\n\t\t\t\t\t\"value\": \"http://google.com\",\r\n\t\t\t\t\t\"short\": false\r\n\t\t\t\t}        \r\n\t\t\t]\r\n\r\n\t\t}\r\n\t])\r\n}\r\nslack.api(\"chat.postMessage\", options, function(err, posted, resp){\r\n        console.log(err, posted, resp);\r\n});\r\n```\r\nit works.\r\n\r\nSeems like this is caused by the special handling of the 'attachments' item.\r\n\r\nThanks for the module, it makes posting to Slack nice and tidy :)\r\n"},"comment":{"url":"https://api.github.com/repos/samuelcouch/slack-attack/issues/comments/157430191","html_url":"https://github.com/samuelcouch/slack-attack/issues/2#issuecomment-157430191","issue_url":"https://api.github.com/repos/samuelcouch/slack-attack/issues/2","id":157430191,"user":{"login":"thecubed","id":846632,"avatar_url":"https://avatars.githubusercontent.com/u/846632?v=3","gravatar_id":"","url":"https://api.github.com/users/thecubed","html_url":"https://github.com/thecubed","followers_url":"https://api.github.com/users/thecubed/followers","following_url":"https://api.github.com/users/thecubed/following{/other_user}","gists_url":"https://api.github.com/users/thecubed/gists{/gist_id}","starred_url":"https://api.github.com/users/thecubed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thecubed/subscriptions","organizations_url":"https://api.github.com/users/thecubed/orgs","repos_url":"https://api.github.com/users/thecubed/repos","events_url":"https://api.github.com/users/thecubed/events{/privacy}","received_events_url":"https://api.github.com/users/thecubed/received_events","type":"User","site_admin":false},"created_at":"2015-11-17T16:56:07Z","updated_at":"2015-11-17T16:56:07Z","body":"I haven't used this library before that change, but it's definitely related.\r\n\r\nChanging those lines to \r\n```\r\nfor (var attrname in options) {\r\n    if (attrname == \"attachments\" && options.attachments instanceof Array) {\r\n        payload.attachments = JSON.stringify(options.attachments);\r\n    } else {\r\n        payload[attrname] = options[attrname];\r\n    }\r\n\r\n};\r\n```\r\nfixes it for me"}}