Is it possible to get a twitter account’s username from an instance of the Twitter REST client? Specifically, I would like to get the name for the twitter account associated with the client’s access tokens and secrets.
I searched through the twitter gem documentation on rubydoc and looked at the Twitter API user object documentation but wasn’t able to solve the problem. I did try using client.attributes
, client.to_h
and client.screen_name
, but received an unknown method error.
For context, I’m currently working on a twitter bot that auto-replies to hashtags when it is looped into a conversation. We want to prevent the bot from tweeting at itself, so we are currently hard-coding in the bot name as an account not to tweet at. It would be helpful if we could replace the hard-coded name with something like client.account_name
.
Thanks for reading.
2
Answers
Taking a look at the documentation for the
client
object of the twitter gem says it has auser
method (Methods included from Users section) [1]. The documentation for theuser
method states it returns aTwitter::User
object of the currently authenticated user [2]. This class inherits fromBasicUser
which is where thescreen_name
method exists [3].You say
and
If I understand you correctly, you want to find out which application was used to send a tweet – so you can filter it out?
In each tweet, you will find an attribute called
source
. This shows which app the user sent the tweet with.For example,
That Tweet was sent using Twitter for Android’s API keys.
And in this example:
The Tweet was sent from my personal API key.
Give your API key a unique name and URL. When you retrieve Tweets, check the
source
parameter, and don’t reply to any which have been sent using your access token and secret.