This is a second part of Twitter Bot project.
This time we will extend it and make it more dynamic. There are lots of public APIs around that we can use. To make things interesting and fun we will stick with Chuck Norris API - a JSON-based API for facts about Chuck Norris!
We will use source code from part 1 as a starting point and will extend it a bit.
Here is a live coding session that shows how it can be done.
Duration: 1:56 (coding - 1:46)
Disclaimer: never use this code in production. It was created for fun.
Let's break down the solution and comment on some complex or interesting things.
0:26 - first step is to remove the hardcoded text of original tweet. It will be replaced with dynamic content.
For this purpose we will call getQuote
function that will be created in a second.
The signature of this function is simple - it does not take any arguments and returns just a string
value.
0:38 - we need to import a couple of built-in Golang modules:
encoding/json
- allows to map JSON string to an objectio/ioutil
- set of utilities to read byte array to a stringnet/http
- to call external HTTP-based services and resources
0:43 - define a structure called Joke
- we will use it to convert a string to a meaningful object.
Here we can notice a couple of Golang-specific things. First of all - properties of the structure should start with upper-case letter (i.e. Value
and not value
).
Also pay attention how mapping from JSON to a value is done using `json:"value"`
. This way it is also possible to map nested structures.
As we are interested only in value
attribute we are mapping only it and ignoring the rest.
0:50 - now we can start calling the external HTTP service.
Technically - it is a GET request. This is very easy to do as the API we use does not require any authentication or API keys.
Good practice is to do error handling as well - we just check that err
is not nil
. This is more or less common approach in Golang.
We should not forget to close the resource we opened. Golang has a nice defer
operator that says that the command should be executed when the current block (in this case - function) ends. It helps to keep your clean-up code closer to when the actual resource initialization is. I recommend to use this approach in your code as well.
1:05 - next we need to read the response of HTTP call to a byte array.
It can be done using ioutil
package.
Here again we need to deal with error handling - quite similar to previous step.
1:14 - converting byte array is super easy as well using json
package.
First we need to define a variable for our Joke
structure and pass it as reference to json.Unmarshal
function call.
Here we do slightly different error handling - a shortcut version where we define and use variable inside if
statement.
1:23 - now we have all information we need and we can return back the composed tweet message. Format it using fmt
package.
1:31 - running the application using go run .
from terminal.
We can see that it successfully authenticated and posted a new tweet.
1:40 - refresh the Twitter page and verify that new tweet has appeared.
This conculdes this video where we've learnt how to call HTTP services using Golang and how to convert (or unmarshal) JSON string values into structures.
Also I would like to thanks authors of https://api.chucknorris.io/ for amazing project they do!
Sources: https://github.com/5minute/twitter_bot_chuck_norris