How did I write the Chatbot to send the Pull Request via ChatWork?

Tram Ho

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

  • Add a gem

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:

basically, don’t reveal this key =))

We can get personal information of this acc chatwork by

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

And this is the result

If you want TO someone, you can TO according to that person’s id

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.

Share the news now

Source : Viblo