chbrown / twttr

Twitter API client for Clojure supporting REST and Streaming endpoints
35 stars 11 forks source link

reply to tweets #3

Closed bullyawareness closed 5 years ago

bullyawareness commented 5 years ago

Hi there, I am new to Clojure and I would like to know how to build a function to reply to tweets containing certain words.

I have been trying to work this out for myself for a few days and getting nowhere.

I am able to send tweets in the repl, I need a function which will let me reply to tweets containing a word. I am not worried about being banned as I will be looking for a very unique set of keywords, I will have it set at ten minute intervals and it is for the purpose of monitering and standing up to online bullying.

Using the function I saw on an earlier issue, I am able to get a list of 15 tweets from keywords

(defn filter-tweets [keyword]
  (api/search-tweets (creds) :params {:q keyword}))

I have been struggling to work out a way of writing a function to respond to the tweets and that is all. I have no issues with creating different tweets to send, however, I am pulling my hair out trying to write a function to respond to different users that I am getting with the filter-tweets function listed above.

Would greatly appreciate any suggestions.

Regards

chbrown commented 5 years ago

That sounds relatively involved. Not all that difficult, but not something I can just bang out in this textbox in a minute or two. Can you be more precise about what problem you're encountering?

Please also see the resources (links) I listed for the user in Issue #2; you might also find them helpful in getting started.

bullyawareness commented 5 years ago

Ok thanks, I will look into the link.

bullyawareness commented 5 years ago

I have made progress, another thing what would be the best way to monitor around 5 keywords, would you suggest search-tweets or to stream? I have had no success with stream do you have an example of this?

Thankyou

chbrown commented 5 years ago

Seems like you'd use a stream, but like you said, you are not worried about being banned, so that's up to you. Here's some code to help you along a bit:

(ns bullyawareness
  "Re: bullyawareness's issue - https://github.com/chbrown/twttr/issues/3"
  (:require [clojure.string :as str]
            [twttr.api :as api]
            [twttr.auth :as auth]))

(def credentials (auth/env->UserCredentials))
(def keywords ["damn" "you" "suck" "hate"])
(def track-stream (api/statuses-filter credentials :params {:track (str/join "," keywords)}))

(defn handle-tweet
  [tweet]
  (println (:screen_name (:user tweet)) "wrote:")
  (println "    " (str/replace (:text tweet) #"\s" " ")))

(run! handle-tweet (take 50 track-stream))