chatwork-api
Version:
Chatwork API for JavaScript
208 lines (156 loc) • 7.21 kB
Markdown
A JavaScript wrapper for [Chatwork APIs](http://developer.chatwork.com/ja/index.html).
```shell
$ npm i -S chatwork-api
```
2 main functions:
* Message Builder: Utility for build chat message easier
* Chatwork API caller: Call Chatwork API
Utility for build chat message easier.
Create a new MessageBuilder instance with initial message and chat room id.
Get current message.
Get current messsge. Same as `toString()`
Set current message.
Append `number` of new lines. Default is 1 line.
Append `message`.
Prepend `message`.
Send message to `account_id`, then append to current message.
Reply to `account_id` with message has id `message_id` in room `room_id`, then append to current message. Default of `room_id` is instance.`room_id`
Quote `message` of `account_id` at `timestamp`, then append to current message. `timestamp` is optional.
Wrap info for `message` with `title`, then append to current message. `title` is optional.
Wrap `hr`, then append to current message.
Show profile icon for `account_id`, then append to current message. When `is_include_name` is `true`, this will show both of profile icon and profile name. The default is `false`.
The wrapper for Chatwork APIs.
* The options for each request is same as Chatwork API options. This reduces the maintain of this lib.
Create new API instance with token, and [request options](https://github.com/request/request#requestoptions-callback).
Get the current [request](https://github.com/request/request) object.
Get current Chatwork api token.
Set Chatwork api token
Set Chatwork api token, and [request options](https://github.com/request/request#requestoptions-callback).
Get [my profile](http://developer.chatwork.com/ja/endpoint_me.html#GET-me).
Get [current status](http://developer.chatwork.com/ja/endpoint_my.html#GET-my-status).
Get [my tasks](http://developer.chatwork.com/ja/endpoint_my.html#GET-my-tasks).
Get [my contacts](http://developer.chatwork.com/ja/endpoint_contacts.html#GET-contacts).
Get [my list chat rooms](http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms).
Create a [new chat room](http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms).
```javascript
options: {
description, // room description
icon_preset, // room icon. Default is `group`
name, // room name
members_admin_ids, // list of admin member ids [Integer, String, Array]
members_member_ids, // list of normal member ids [Integer, String, Array]
members_readonly_ids // list of readonly member ids [Integer, String, Array]
}
```
Get [chat room detail](http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id).
Update [chat room information](http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id).
```javascript
options: {
description, // room description
icon_preset, // room icon. Default is `group`
name // room name
}
```
Leave [chat room](http://developer.chatwork.com/ja/endpoint_rooms.html#DELETE-rooms-room_id).
Delete [chat room](http://developer.chatwork.com/ja/endpoint_rooms.html#DELETE-rooms-room_id).
Get [list members](http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-members) of a chat room.
Update list of [members](http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-members) for a chat room.
```javascript
options: {
members_admin_ids, // list of admin member ids [Integer, String, Array]
members_member_ids, // list of normal member ids [Integer, String, Array]
members_readonly_ids // list of readonly member ids [Integer, String, Array]
}
```
Get [list messages](http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-messages).
Get [message information](http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-messages-message_id).
Send a [new message](http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-messages).
Get [list taks](http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-tasks).
```javascript
options: {
account_id, // owner account id
assigned_by_account_id, // assigned account id
status // task content. Default is `open`
}
```
Get [task information](http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-tasks-task_id).
Create a [new task](http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-tasks).
```javascript
options: {
body, // task content
limit, // task expiration
to_ids // assign to list of account_ids. [Integer, String, Array]
}
```
Get [list files](http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-files).
Get [file information](http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-files-file_id). Default of `doCreateDownloadUrl` is false.
```javascript
const API = require('chatwork-api', {
proxy: 'http://USER_NAME:USER_PASS@PROXY_URL:PROXY_PORT/'
})
const MessageBuilder = API.MessageBuilder
const STATUS = API.CONST.STATUS
// create new API instance with api token
let api = new API('YOUR_TOKEN_HERE')
// get my task
api.myTasks('ASSIGNED_BY_ACCOUNT_ID', STATUS.DONE)
.then(console.log)
.catch(console.error)
// create a new message with MessageBuilder
let builder = new MessageBuilder()
builder.to('ACCOUNT_ID') // send to ACCOUNT_ID
.append('Wellcome to this') // append message
.append('世界')
builder.n(2) // add 2 new lines
.append('Awesome ')
.profileIcon('ACCOUNT_ID', true) // add profile icon with user name
// send message in ROOM_ID
api.sendMessage('ROOM_ID', builder.message)
.then(console.log)
.catch(console.error)
```