{"id":734,"date":"2021-08-26T10:28:09","date_gmt":"2021-08-26T01:28:09","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=734"},"modified":"2021-08-26T11:09:55","modified_gmt":"2021-08-26T02:09:55","slug":"getting-started-with-chatwork-api","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=734","title":{"rendered":"Getting Started with Chatwork API"},"content":{"rendered":"<p>This is a blog post with simple samples for getting started with interacting with the ChatWork API. Information on the API itself can be found <a href=\"https:\/\/download.chatwork.com\/ChatWork_API_Documentation.pdf\">here<\/a> and details on the NPM library we are using can be found <a href=\"https:\/\/www.npmjs.com\/package\/chatwork-api-client\">here<\/a>. The examples covered in this repository are as follows. The code for this post can be found here: <a href=\"https:\/\/github.com\/wsdCollins\/Chatwork-HelloWorld\">https:\/\/github.com\/wsdCollins\/Chatwork-HelloWorld<\/a>.<br \/>\n1. Post a message<br \/>\n2. Post a file<br \/>\n3. Get and write file<\/p>\n<p>Get an API Token<\/p>\n<p>This is a required step for any of the following steps to work, which requires getting an API token from the ChatWork settings after logging in. First clone this repository.<\/p>\n<pre>$ git clone https:\/\/github.com\/wsdCollins\/Chatwork-HelloWorld.git\r\n$ cd Chatwork-HelloWorld\r\n$ npm install<\/pre>\n<p>Also install TypeScript globally if you haven&#8217;t already. This is required for compiling the examples.<\/p>\n<p>$ sudo npm install typescript -g<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/5259968\/130886146-10c25982-08cb-4ac4-af4a-6b082577516a.png\"\/><\/p>\n<p>From the chat main screen, click on the top right drop down menu and slect &#8220;Integrations&#8221;.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/5259968\/130886151-73a04f7f-9c31-42ae-9eb8-073fba3120b8.png\"\/><\/p>\n<p>A new tab will open. Under the API section on the right hand menu, select &#8220;API Token&#8221;. Enter your password and click on &#8220;Display&#8221;.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/5259968\/130886155-dae41224-0dc6-4e07-9b87-1b20940af681.png\"\/><\/p>\n<p>This token will allow you to interact with the API. In the cloned repository create a &#8220;.env&#8221; file and replace &#8220;[YOUR_API_TOKEN]&#8221; with the copied value of your API token.<\/p>\n<pre>$ echo \"CHATWORK_TOKEN=[YOUR_API_TOKEN]\" > .env<\/pre>\n<h3>Post a Message<\/h3>\n<p>For the first example, we will do a classic &#8220;Hello World&#8221; to see if we can post a message to the default &#8220;MyRoom&#8221; included with each new account. First we compile the &#8220;`hello.ts&#8220; file.<\/p>\n<pre>$ tsc hello.ts<\/pre>\n<p>And then we run with:<\/p>\n<pre>$ node hello.js\r\nArren Maxwell\r\n[\r\n  {\r\n    room_id: 241035249,\r\n    name: '\u30de\u30a4\u30c1\u30e3\u30c3\u30c8',\r\n    type: 'my',\r\n    role: 'member',\r\n    sticky: true,\r\n    unread_num: 0,\r\n    mention_num: 0,\r\n    mytask_num: 0,\r\n    message_num: 11,\r\n    file_num: 8,\r\n    task_num: 0,\r\n    icon_path: 'https:\/\/appdata.chatwork.com\/avatar\/ico_default_green.png',\r\n    last_update_time: 1629942433\r\n  }\r\n]\r\n{ message_id: '1482556736417079296' }<\/pre>\n<p>The result should look like the following.<br \/>\n<img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/5259968\/130887280-0b253439-f6b6-458e-ae69-bbd17c9d8b72.png\"\/><\/p>\n<h3>Post a File<\/h3>\n<p>The second example is posting a file. We will post two files. And example png with the WSD logo, and a dummy pdf file to show the sample works with different file types. These files are included in the data folder. First we compile the uploadFile.ts file.<\/p>\n<pre>$ tsc uploadFile.ts<\/pre>\n<p>And then we run with:<\/p>\n<pre>$ node uploadFile.js\r\nArren Maxwell\r\n[\r\n  {\r\n    room_id: 241035249,\r\n    name: '\u30de\u30a4\u30c1\u30e3\u30c3\u30c8',\r\n    type: 'my',\r\n    role: 'member',\r\n    sticky: true,\r\n    unread_num: 0,\r\n    mention_num: 0,\r\n    mytask_num: 0,\r\n    message_num: 12,\r\n    file_num: 8,\r\n    task_num: 0,\r\n    icon_path: 'https:\/\/appdata.chatwork.com\/avatar\/ico_default_green.png',\r\n    last_update_time: 1629942670\r\n  }\r\n]\r\n{ file_id: 788547100 }\r\n{ file_id: 788547105 }<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/5259968\/130887789-49b17787-92e1-4db2-9d42-f5571f472eeb.png\"\/><\/p>\n<h3>Download a File<\/h3>\n<p>The third example is to download a file. In this case we have a sample.json file that is included in the data folder. We want to first manually drag and drop this into our &#8220;MyRoom&#8221; to be able to download it.<br \/>\n<img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/5259968\/130887937-157156d2-c177-49ed-8ff3-41f7fd9c3030.png\"\/><\/p>\n<p>Next we will download this file and write is as out.json in the data folder, so that we know we have a new file that has been downloaded. To do this we will compile the downloadFile.ts file.<\/p>\n<pre>$ tsc downloadFile.ts<\/pre>\n<p>And then we run with<\/p>\n<pre> $ node downloadFile.js\r\nArren Maxwell\r\n[\r\n  {\r\n    room_id: 241035249,\r\n    name: '\u30de\u30a4\u30c1\u30e3\u30c3\u30c8',\r\n    type: 'my',\r\n    role: 'member',\r\n    sticky: true,\r\n    unread_num: 0,\r\n    mention_num: 0,\r\n    mytask_num: 0,\r\n    message_num: 15,\r\n    file_num: 11,\r\n    task_num: 0,\r\n    icon_path: 'https:\/\/appdata.chatwork.com\/avatar\/ico_default_green.png',\r\n    last_update_time: 1629943196\r\n  }\r\n]\r\n--- Got the Files ---\r\n[\r\n  {\r\n    file_id: 788547100,\r\n    message_id: '1482558473966190592',\r\n    filesize: 32829,\r\n    filename: 'CompanyLogo.png',\r\n    upload_time: 1629943082,\r\n    account: {\r\n      account_id: 6196123,\r\n      name: 'Arren Maxwell',\r\n      avatar_image_url: 'https:\/\/appdata.chatwork.com\/avatar\/ico_default_green.png'\r\n    }\r\n  },\r\n  {\r\n    file_id: 788547105,\r\n    message_id: '1482558478965841920',\r\n    filesize: 13264,\r\n    filename: 'dummy.pdf',\r\n    upload_time: 1629943082,\r\n    account: {\r\n      account_id: 6196123,\r\n      name: 'Arren Maxwell',\r\n      avatar_image_url: 'https:\/\/appdata.chatwork.com\/avatar\/ico_default_green.png'\r\n    }\r\n  },\r\n  {\r\n    file_id: 788549337,\r\n    message_id: '1482558942260273153',\r\n    filesize: 729,\r\n    filename: 'sample.json',\r\n    upload_time: 1629943195,\r\n    account: {\r\n      account_id: 6196123,\r\n      name: 'Arren Maxwell',\r\n      avatar_image_url: 'https:\/\/appdata.chatwork.com\/avatar\/ico_default_green.png'\r\n    }\r\n  }\r\n]\r\n --- File Response with Download URL ---\r\n{\r\n  file_id: 788549337,\r\n  message_id: '1482558942260273153',\r\n  filesize: 729,\r\n  filename: 'sample.json',\r\n  upload_time: 1629943195,\r\n  account: {\r\n    account_id: 6196123,\r\n    name: 'Arren Maxwell',\r\n    avatar_image_url: 'https:\/\/appdata.chatwork.com\/avatar\/ico_default_green.png'\r\n  },\r\n  download_url: 'https:\/\/appdata.chatwork.com\/uploadfile\/241035\/241035249\/4fd7749e57a32c8728925a578ba3c568.dat?response-content-type=&response-content-disposition=attachment%3Bfilename%2A%3DUTF-8%27%27sample.json&Expires=1629943345&Signature=mNDrcEOd4tcWvmZnI3wLPlUJAI7yryk74VzQNtHQL6t-bJTda7VpotiY4Aiv9A~8awB~VCwrVaknji4pEBiR4FkL2DLdE~RzrdBYfSIhOdqp04jakRh9FEfkDrpXIhO7GLUDT6ARORHapikbYKayYyhA~NaxHK3RIXJWNHoOw1~ETTXD-VBL-JP8Xf1c3XdXJNF9wMDB6BUI9TMiTskSX9r62nkjRNrLXXNy35rLaVR4uJyM03MLrr1JpFNX4xy4q-eKaYQ7Z-zJH-uQDL46Ov~7ceez7z1Z11fAch2UTLdOEDthZgTgLSe4ziyMaL1DNSmo3tucly3Cyds208EJWQ__&Key-Pair-Id=APKAIZEFQUITKUSISS7A'\r\n}<\/pre>\n<p>From there if we check the data folder, we should see a new file names out.json with the following content.<\/p>\n<pre>{\r\n    \"@context\": \"https:\/\/schema.org\/\",\r\n    \"@type\": \"Invoice\",\r\n    \"description\": \"January 2015 Visa bill.\",\r\n    \"url\": \"http:\/\/acmebank.com\/invoice.pdf\",\r\n    \"broker\": {\r\n      \"@type\": \"BankOrCreditUnion\",\r\n      \"name\": \"ACME Bank\"\r\n    },\r\n    \"accountId\": \"xxxx-xxxx-xxxx-1234\",\r\n    \"customer\": {\r\n      \"@type\": \"Person\",\r\n      \"name\": \"Jane Doe\"\r\n    },\r\n    \"paymentDueDate\": \"2015-01-30T12:00:00\",\r\n    \"minimumPaymentDue\": {\r\n      \"@type\": \"PriceSpecification\",\r\n      \"price\": 15.00,\r\n      \"priceCurrency\": \"USD\"\r\n    },\r\n    \"totalPaymentDue\": {\r\n      \"@type\": \"PriceSpecification\",\r\n      \"price\": 200.00,\r\n      \"priceCurrency\": \"USD\"\r\n    },\r\n    \"billingPeriod\": \"P30D\",\r\n    \"paymentStatus\": \"https:\/\/schema.org\/PaymentDue\"\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a blog post with simple samples for getting started with interacting with the ChatWork API. Informatio&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/734"}],"collection":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=734"}],"version-history":[{"count":4,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/734\/revisions"}],"predecessor-version":[{"id":739,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/734\/revisions\/739"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}