Created
August 10, 2017 23:40
-
-
Save Roach/474eb177290400b3f05d18ab191ef888 to your computer and use it in GitHub Desktop.
postMessage.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Sends a message to a channel. | |
* @param username Set your bot's user name. Must be used in conjunction with `as_user` set to false, otherwise ignored. See [authorship](#authorship) below. | |
* @param threadTs Provide another message's `ts` value to make this message a reply. Avoid using a reply's `ts` value; use its parent instead. | |
* @param attachments Structured message attachments. | |
* @param unfurlLinks Pass true to enable unfurling of primarily text-based content. | |
* @param text Text of the message to send. See below for an explanation of [formatting](#formatting). This field is usually required, unless you're providing only `attachments` instead. | |
* @param unfurlMedia Pass false to disable unfurling of media content. | |
* @param parse Change how messages are treated. Defaults to `none`. See [below](#formatting). | |
* @param asUser Pass true to post the message as the authed user, instead of as a bot. Defaults to false. See [authorship](#authorship) below. | |
* @param token Authentication token. Requires scope: `chat:write` | |
* @param iconEmoji Emoji to use as the icon for this message. Overrides `icon_url`. Must be used in conjunction with `as_user` set to `false`, otherwise ignored. See [authorship](#authorship) below. | |
* @param linkNames Find and link channel names and usernames. | |
* @param iconUrl URL to an image to use as the icon for this message. Must be used in conjunction with `as_user` set to false, otherwise ignored. See [authorship](#authorship) below. | |
* @param channel Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name. See [below](#channels) for more details. | |
* @param replyBroadcast Used in conjunction with `thread_ts` and indicates whether reply should be made visible to everyone in the channel or conversation. Defaults to `false`. | |
*/ | |
public chatPostMessage (username?: string, threadTs?: number, attachments?: string, unfurlLinks?: boolean, text?: string, unfurlMedia?: boolean, parse?: string, asUser?: boolean, token?: string, iconEmoji?: string, linkNames?: boolean, iconUrl?: string, channel?: string, replyBroadcast?: boolean) : Promise<{ response: http.ClientResponse; body?: any; }> { | |
const localVarPath = this.basePath + '/chat.postMessage'; | |
let queryParameters: any = {}; | |
let headerParams: any = (<any>Object).assign({}, this.defaultHeaders); | |
let formParams: any = {}; | |
let useFormData = false; | |
if (username !== undefined) { | |
formParams['username'] = username; | |
} | |
if (threadTs !== undefined) { | |
formParams['thread_ts'] = threadTs; | |
} | |
if (attachments !== undefined) { | |
formParams['attachments'] = attachments; | |
} | |
if (unfurlLinks !== undefined) { | |
formParams['unfurl_links'] = unfurlLinks; | |
} | |
if (text !== undefined) { | |
formParams['text'] = text; | |
} | |
if (unfurlMedia !== undefined) { | |
formParams['unfurl_media'] = unfurlMedia; | |
} | |
if (parse !== undefined) { | |
formParams['parse'] = parse; | |
} | |
if (asUser !== undefined) { | |
formParams['as_user'] = asUser; | |
} | |
if (token !== undefined) { | |
formParams['token'] = token; | |
} | |
if (iconEmoji !== undefined) { | |
formParams['icon_emoji'] = iconEmoji; | |
} | |
if (linkNames !== undefined) { | |
formParams['link_names'] = linkNames; | |
} | |
if (iconUrl !== undefined) { | |
formParams['icon_url'] = iconUrl; | |
} | |
if (channel !== undefined) { | |
formParams['channel'] = channel; | |
} | |
if (replyBroadcast !== undefined) { | |
formParams['reply_broadcast'] = replyBroadcast; | |
} | |
let requestOptions: request.Options = { | |
method: 'POST', | |
qs: queryParameters, | |
headers: headerParams, | |
uri: localVarPath, | |
useQuerystring: this._useQuerystring, | |
json: true, | |
}; | |
this.authentications.default.applyToRequest(requestOptions); | |
if (Object.keys(formParams).length) { | |
if (useFormData) { | |
(<any>requestOptions).formData = formParams; | |
} else { | |
requestOptions.form = formParams; | |
} | |
} | |
return new Promise<{ response: http.ClientResponse; body?: any; }>((resolve, reject) => { | |
request(requestOptions, (error, response, body) => { | |
if (error) { | |
reject(error); | |
} else { | |
if (response.statusCode >= 200 && response.statusCode <= 299) { | |
resolve({ response: response, body: body }); | |
} else { | |
reject({ response: response, body: body }); | |
} | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment