Created
June 29, 2013 22:18
-
-
Save ray1729/5892911 to your computer and use it in GitHub Desktop.
Sending tweets from Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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