This is Peng.
Today I want to talk about how to get and send messages to Chatwork with python.
It is simple to realize with python library requests
and Chatwork API support.
Chatwork API
Chatwork API is an API provided for developers to programmatically interact with Chatwork's services. It enables you to integrate Chatwork’s features into your own web application and business system. Chatwork API can be used to make a web server notify the administrator when an error occurs, or make a project management tool send out messages to other Chatwork users when tasks status are updated.
Before start
Before we start, there are three const that we need to get in hand first.
- Chatwork base URL
- Account Token
- Room ID
The basic Url of chatwork API is BASE_URL = 'https://api.chatwork.com/v2/'
.
To access Chatwork, we need our Chatwork Token of account. Each account has its own token to access Chatwork. You can check or apply your token here.
Of course, we need Room ID in chatwork channel to send or get messages from.
https://www.chatwork.com/#!rid206xxxxxx
The number 206xxxxxx after the #!rid is our room ID, each room has an unique ID.
With personal token and chatwork API url, the request will be made by the name of our account to get messages or send messages to specific rooms.
So, we have these 3 necessary parts in hand (TOKEN, ROOM_ID, BASE_URL), let's begin!
Send message to Chatwork
What we do is to combine BASE_URL with our requests to send messages to certain rooms.
Let say our message is s = "Hello world"
, then we just simple execute this code...
import requests res = requests.post( BASE_URL + 'rooms/{}/messages'.format(ROOM_ID)), headers={'X-ChatWorkToken': TOKEN}, params={'body': s} )
DONE!
Isn't it very easy?
We don't need to use res
variable in later code because it contains the POST response message from Chatwork which are the informations that we don't need. But it is necessary to assign the result of requests.post(....)
to a variable.
Get message from Chatwork room
Similar to the sending message code, to get message from certain room, these codes will satisfy:
res = requests.get( BASE_URL + 'rooms/{}/messages'.format(ROOM_ID), headers={'X-ChatWorkToken': TOKEN} )
But when you execute that codes, it will return recent 100 response as a list, of which contains each messages. It seems fine ,but when you run that again, you will get an empty list.
WHY?
Because when Chatwork API receives the GET request from our program, it initially returns the unread messages (maximum 100) to us (which at the first trial all messages are considered unread). Chatwork will then mark those messages as read, so the next time we send request to it , it will return an empty list.
It sounds good for those who only want unread messages in their program, but what if we need those read messages as well?
In the request URL , there is a default suffix attached to the url?force=0
, which means do not get those read messages.
To get recent 100 messages , what we do is revise the code to :
import requests force = True // True: Get all recent 100 messages, False: get only new message force_flg = '?force=1' if force else '?force=0' res = requests.get( BASE_URL + 'rooms/{}/messages'.format(ROOM_ID)+force_flag, headers={'X-ChatWorkToken': TOKEN} )
This time we need the content in variable res
. Because res
is a list consist of 100(maximum) response, each of which will have similar structure like this:
If we want to get recent 5 messages no matter read or unread, we can do it this way:
import requests force = True // True: Get all recent 100 messages, False: get only new message force_flg = '?force=1' if force else '?force=0' res = requests.get( BASE_URL + 'rooms/{}/messages'.format(ROOM_ID)+force_flag, headers={'X-ChatWorkToken': TOKEN} ) recent_n = 5 raw_text = res if len(raw_text) != 0: recent_n_msg = [i["body"] for i in raw_text[-recent_n:]] for i in range(len(recent_n_msg)): // some operations to these messages. else: print("NO MESSAGES IN THIS ROOM")
Conclusion
In this blog, I introduce how to send and get messages from Chatwork with Python and its requests
library. The hidden default parameter of get messages from Chatwork Room is also introduced.
To sum up, this is not difficult to implement, I still hope you learn something from it. :)