A Twitter bot example in Clojure
-
Make sure you have followed the Lemmings Clojure and Atom Editor setup.
-
Start a new vagrant session by opening a new terminal window (on Windows you can use Git BASH), navigate to the 'clojure-master' directory you downloaded before which contains the
Vagrantfile
and runvagrant ssh
. -
In the VM, change into the projects directory with
cd projects/
and start with the tutorial. -
"Clone" the Twitter example into your Vagrant environment
git clone https://github.com/lemmings-io/01-twitter-example.git
Now change into the twitter example directory:
cd 01-twitter-example
-
Start the nREPL server
Start another VM session by opening a new terminal window (Cmd-T) and run
vagrant ssh
.Navigate to the project folder you just created with
cd projects/01-twitter-example
.Start the nREPL server with
lein repl :headless :host 0.0.0.0 :port 7888
as described in the Clojure and Atom Editor Setup -
Open your project in Atom.
Start Atom and open the folder
clojure-master/projects/01-twitter-example
as an Atom project.There's two things to know about Atom:
-
Ctrl-P on Windows and Cmd-P on macOS opens a file search. You can use this to jump to any file in your project quickly.
-
Ctrl-Shift-P on Windows and Cmd-Shift-P on macOS opens the Atom Command Palette. You use it to start the nREPL or enable Autoeval.
-
-
Connect to Atom's nREPL.
Look at the Lemmings Clojure guide for detailled instructions.
-
Turn on the "Autoeval" feature of Proto Repl:
Open the
core.clj
file in Atom, then open Atom command palette and search for "Proto Repl: Autoeval file" and press return.Hint: You can find the
core.clj
file insrc/twitter_example/core.clj
, but if you press Ctrl-P on Windows or Cmd-P on macOS you can easily search through all files in your project.This feature will take care to synchronize your code in the
core.clj
file with the Clojure REPL. -
Artistic Tweaking
You can see the lemmings bot in action here.
However, you probably want to personalise your tweets now :)
In your
core.clj
you'll find two lists:templates
(the skeleton of your tweets) andblanks
(the strings you'll fill the blanks with). You can replace these strings with your own, just remember to keep the___
.You can test the generated sentences in your REPL following these steps:
-
Let's bring it to life! Provide your Twitter API tokens for local development by creating a file called
profiles.clj
in01-twitter-example
with Atom.You can find instructions for signing up for Twitter and getting these tokens in the Twitter Bot example in the section Hooking the bot up to Twitter.
{:dev {:env {:app-consumer-key "<REPLACE>" :app-consumer-secret "<REPLACE>" :user-access-token "<REPLACE>" :user-access-secret "<REPLACE>"}}}
-
Test if your bot tweets in Atom's nREPL
Run
(ns twitter-example.core)
to initialize your app's namespace in the nREPL.Then run
(tweet-sentence)
in the nREPL to send a status update through your twitter account. -
Start the twitter bot inside Vagrant:
In the Terminal where you are connected to Vagrant (
vagrant ssh
) runlein trampoline run
to start a Clojure system that will tweet every 2 hours.
This example is based on the Twitter Bot Example in Clojure by Carin Meier.
Copyright © 2017
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.