Skip to content

Instantly share code, notes, and snippets.

@bz0
Last active January 6, 2018 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bz0/9702c3230f043adee3031cb6e1e9cc33 to your computer and use it in GitHub Desktop.
Save bz0/9702c3230f043adee3031cb6e1e9cc33 to your computer and use it in GitHub Desktop.
slackにメッセージを通知する

promise

tscコンパイル前に、promiseも変換できるようにtsconfig.json内に 「es2015.promise」を追記します。

"lib" : ["es2015", "es2015.iterable", "dom", "es2015.promise"]

その後、tscを実行して下さい。

class slack {
private path = "https://slack.com/api/";
private request(url:string):any {
return new Promise(resolve => {
let xhr = new XMLHttpRequest();
xhr.onload = ()=>{
resolve(JSON.parse(xhr.response));
};
xhr.open("GET", url);
xhr.send();
});
}
private urlParametor(params:{[key:string]:string}):string {
let queryArray: Array<string> = new Array();
for(var key in params){
console.log(key + '=' + encodeURIComponent(params[key]));
queryArray.push(key + '=' + encodeURIComponent(params[key]));
}
const query = queryArray.join('&');
return query;
}
public async postMessage(params:{[key:string]:string}){
const method = "chat.postMessage";
const query = this.urlParametor(params);
const url = this.path + method + "?" + query;
let res:any = await this.request(url);
return res;
}
}
const s = new slack();
const params:{[key:string]:string} = {
"token": "xoxp-xxxxxxx",
"text": "test!!!!",
"channel": "general"
};
const res = s.postMessage(params);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment