I started this blog in order to learn how to use Twitter API and also because i love going to the cinema :raised_hands:. Also it’s a good opportunity to learn how to Create a Twitter Bot with Phoenix. First thing first, Im from Uruguay 🇺🇾, and we have a cinema called «Movie Center». After checking their request, I started to investigate what we can get from them
Investigation 🕵️
After checking their request I found out that I can use that information in order to create the Twitter bot.




Creating the app 👨💻
Let’s start by creating our Phoenix app
mix phx.new movies_uy
cd movies_uy
mix ecto.create
iex -S mix phx.server
Now Im going to add the following dependencies that are needed for the bot:

- Finch for doing request to the «Movie Center» API.
- ExTwitter it’s a Twitter Client for using Twitter API
- Oban for creating jobs that run whenever we want (in our case once a day)
After adding this don’t forget to run mix deps.get
.
Config Finch
Let’s configure Finch by adding this, note that you can follow this from the Finch repo too.

Config ExTwitter 🧵

Getting Movies for our twitter bot 🎬
Let’s add the following code that will search for all the movies from the Movie Center API.

You can also check if this is working correctly by running the following line on your iex
$ iex> MoviesUy.MovieCenter.fetch_movies # this should show all the movies from Movie center API
Using ExTwitter 🧵
Now is time to build the messages and post them on Twitter using ExTwitter.
What Im going to do is create a tweet that contains the current date, and the comments of this twitter will be the movies with their current url on Movie Center.

We can run this using the same function as before and we should be able to see that is posting correctly on Twitter.


Config Oban 📅
It’s time to use Oban, this will allow us to create a worker that runs our code whenever we want, in our case once a day! Lets config Oban (you can also read the docs).
$ mix ecto.gen.migration add_oban_jobs_table
Then open the generated migration and update it:

And finally lets run this
$ mix ecto.migrate
Lets add this



Thats it! you can see this Twitter bot working visiting: https://twitter.com/movies_uy
Note: you can also create just a mix app and this guide will work too. I used
phx
because Im planning to do another thing in the future with it
And this is how you can create a Twitter Bot with Phoenix, hope you enjoyed this step by step guide!
Things we used here:
- https://github.com/sneako/finch
- https://github.com/parroty/extwitter
- https://github.com/sorentwo/oban
Visit the twitter bot:
Twitter bot: https://twitter.com/movies_uy
The twitter code bot:
https://github.com/kexposito/movies-uy
Si te gustó, no olvides de suscribirte para más contenido!