Skip to content

Instantly share code, notes, and snippets.

@ray1729
Created June 29, 2013 22:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ray1729/5892911 to your computer and use it in GitHub Desktop.
Save ray1729/5892911 to your computer and use it in GitHub Desktop.
Sending tweets from Clojure
(require '[clojure.java.io :as io])
(require '[oauth.twitter :refer [oauth-client]])
(defn read-properties
"Parse a properties file, convert the property keys to Clojure
keywords and return as a Clojure map."
[resource-name]
(when-let [resource (io/resource resource-name)]
(let [properties (java.util.Properties.)]
(with-open [stream (io/input-stream resource)]
(.load properties stream))
(zipmap (map keyword (keys properties))
(vals properties)))))
(defn client
"Create an OAuth client for the Twitter API, using the supplied `credentials`."
[credentials]
(ouath-client (:consumer-key credentials)
(:consumer-secret credentials)
(:access-token credentials)
(:access-token-secret credentials)))
(defn tweet
"Tweet a status update."
[twitter-client status]
(twitter-client {:method :post
:url "https://api.twitter.com/1.1/statuses/update.json"
:form-params {:status status}}))
;; Read Twitter credentials from a properties file
(def credentials (read-properties "twitter.properties"))
;; Create a Twitter client with the specified credentials
(def twitter-client (client credentials))
;; Send our first tweet!
(tweet twitter-client "My first tweet")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment