chatwork-api

1.0.0 • Public • Published

chatwork-api

A JavaScript wrapper for Chatwork APIs.

Current Version: v1.0 Preview

Install

$ npm i -S chatwork-api

Usage

2 main functions:

  • Message Builder: Utility for build chat message easier
  • Chatwork API caller: Call Chatwork API

Message Builder

Utility for build chat message easier.

Builder(message, roomId)

Create a new MessageBuilder instance with initial message and chat room id.

builder.toString()

Get current message.

builder.message

Get current messsge. Same as toString()

builder.message=

Set current message.

builder.n(number)

Append number of new lines. Default is 1 line.

builder.append(message)

Append message.

builder.prepend(message)

Prepend message.

builder.to(account_id)

Send message to account_id, then append to current message.

builder.reply(account_id, message_id, room_id)

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

builder.quote(account_id, message, timestamp)

Quote message of account_id at timestamp, then append to current message. timestamp is optional.

builder.info(message, title)

Wrap info for message with title, then append to current message. title is optional.

builder.hr()

Wrap hr, then append to current message.

builder.profileIcon(account_id, is_include_name)

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.

Chatwork APIs

The wrapper for Chatwork APIs.

  • The options for each request is same as Chatwork API options. This reduces the maintain of this lib.
API(token, options)

Create new API instance with token, and request options.

api.request

Get the current request object.

api.token

Get current Chatwork api token.

api.token=

Set Chatwork api token

api.set(token, options)

Set Chatwork api token, and request options.

api.me()

Get my profile.

api.myStatus()

Get current status.

api.myTasks(aid, status)

Get my tasks.

api.contacts()

Get my contacts.

api.rooms()

Get my list chat rooms.

api.createRoom(options)

Create a new chat room.

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]
}
api.room(room_id)

Get chat room detail.

api.updateRoom(room_id, options)

Update chat room information.

options: {
  description, // room description
  icon_preset, // room icon. Default is `group`
  name         // room name
}
api.exitRoom(room_id)

Leave chat room.

api.delRoom(room_id)

Delete chat room.

api.members(room_id)

Get list members of a chat room.

api.updateMembers(room_id, options)

Update list of members for a chat room.

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]
}
api.messages(room_id, force)

Get list messages.

api.message(room_id, message_id)

Get message information.

api.sendMessage(room_id, body)

Send a new message.

api.tasks(room_id, options)

Get list taks.

options: {
  account_id,              // owner account id
  assigned_by_account_id,  // assigned account id
  status                   // task content. Default is `open`
}
api.task (room_id, task_id)

Get task information.

api.createTask(room_id, options)

Create a new task.

options: {
  body,  // task content
  limit, // task expiration
  to_ids // assign to list of account_ids. [Integer, String, Array]
}
api.files(room_id, uploaded_account_id)

Get list files.

api.file(room_id, file_id, doCreateDownloadUrl)

Get file information. Default of doCreateDownloadUrl is false.

Examples

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)

Readme

Keywords

Package Sidebar

Install

npm i chatwork-api

Weekly Downloads

4

Version

1.0.0

License

MIT

Last publish

Collaborators

  • dominhhai