Deployed always
Webhooks and webhooks in github
Let’s talk about the webhook concept first. Basically, as I understand, the webhook is an external request sent to our system that we need the data of that request to serve the desired request. Readers can read here to understand and understand how the webhook works.
Like other systems, github programmers provide webhooks for payloads of events on git.
Interact with Chatwork
Currently, I use the rails backend to process requests for payloads sent by the webhook github.
In the current rails there is a very good support gem for you to easily send messages to chatwork. You can read here to understand and know how to use listening: v
So what is the basis here, I go through a few simple steps:
- First of all, it is necessary to have a rails project
1 2 | rails new chat_bot |
- Add a gem
1 2 | gem "chatwork" |
We can use rails c to demo some primitive things. Oh, here I remember that we need an acc chatwork to perform the operation, right: v Can go here to get the api key of the chatwork, start he, set api_key by the following:
1 2 3 | 2.5.0 :001 > ChatWork.api_key = "aa208c1pa1442b8febffl283a7ddc4z7" => "aa208c1pa1442b8febff7983a7ddc4e7" |
basically, don’t reveal this key =))
We can get personal information of this acc chatwork by
1 2 3 | 2.5.0 :002 > ChatWork::Me.get => #<Hashie::Mash account_id=5158557 avatar_image_url="https://appdata.chatwork.com/avatar/4737/4737813.rsz.jpg" chatwork_id="" department="" facebook="tranloc1997" introduction="" login_mail=" <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> " mail="" name="[bot] SSV" organization_id=3708272 organization_name="" room_id=198328272 skype="" tel_extension="" tel_mobile="" tel_organization="" title="Em anh Lộc :v" twitter="" url=""> |
Usually during the process of using chatwork, we often join the chat rooms, a link of the room has the following form https://www.chatwork.com/#!rid190328208
. Then the suffix 190328208 here is the room_id of that room. When sending content to any group, the required element is room_id
Suppose I want to send the text “Hello everyone!” to the room above, just a simple operation
1 2 3 | 2.5.0 :003 > ChatWork::Message.create room_id: "190328208", body: "Xin chào các bạn!" => #<Hashie::Mash message_id="1360263275979251712"> |
And this is the result
If you want TO someone, you can TO according to that person’s id
1 2 3 | 2.5.0 :005 > ChatWork::Message.create room_id: "190328208", body: "[To:3130608]BigCityBoy nXin chào!" => #<Hashie::Mash message_id="1360264715921543168"> |
And this is the result
So through a few simple steps we can interact from our server to the chatwork.
Handling payloads from github
Go to github and create a payload to use
In the Payload URL
we can enter the action that handles the payload we have, but how to send it if we are local, hehe. I know of a so-called ngrok that helps to quickly public my local to the internet without deploying, come here to read and use.
I choose the option Let me select individual events.
because I only need to use a few events of git only. Suppose I want to comment every time and PR will TO someone, I Issue comments
the Issue comments
.
After this event, the webhook github will send a request to its server with the payload params of that operation. Whatever I handle, just write: v
-> Update webhook
Conclusion
Through 2 simple introduction just now, I think you guys understand somewhat about the mechanism of what I’m talking about. You can go here to read my demo, the code is very bananas, do not criticize: v. Also learn to quickly make a bot to serve the project, so I code through the speakers and do not in-depth research on the arrays, with errors, you can comment so that I can gradually correct towards more quality articles for you to read later .
Thank you for reading your sharing.