Is there a way to access the original tweets from reply tweets? - twitterapi-python

I am currently working on a project using the twitter api and I have a dataset of reply tweets containing a certain word. Is there a way to access the original i.e. main tweets from these reply tweets?

Welcome to StackOverflow, serenayyildiz
You could find the original tweet from one of its replies using tweepy library:
auth = tweepy.OAuthHandler(key, secret)
auth.set_access_token(token, token_secret)
api = tweepy.API(auth)
with open('tweets.csv','w') as f1:
writer = csv.writer(f1)
for reply_id in list_of_replies:
reply_tweet = api.get_status(id=reply_id)
original_tweet_id = reply_tweet.in_reply_to_status_id
original_tweet = api.get_status(original_tweet_id )
row = original_tweet.text
writer.writerow(row)
Here are the list of attributes in the Status object, returned when you call api.get_status:
created_at : The time the status was posted.
id : The ID of the status.
id_str : The ID of the status as a string.
text : The text of the status.
entities : The parsed entities of the status such as hashtags, URLs etc.
source : The source of the status.
source_url : The URL of the source of the status.
in_reply_to_status_id : The ID of the status being replied to.
in_reply_to_status_id_str : The ID of the status being replied to in as a string.
in_reply_to_user_id : The ID of the user being replied to.
in_reply_to_user_id_str : The ID of the user being replied to as a string.
in_reply_to_screen_name : The screen name of the user being replied to
user : The User object of the poster of the status.
geo : The geo object of the status.
coordinates : The coordinates of the status.
place : The place of the status.
contributors : The contributors of the status.
is_quote_status : Indicates whether the status is a quoted status or not.
retweet_count : The number of retweets of the status.
favorite_count : The number of likes of the status.
favorited : Indicates whether the status has been favourited by the authenticated user or not.
retweeted : Indicates whether the status has been retweeted by the authenticated user or not.
possibly_sensitive : Indicates whether the status is sensitive or not.
lang : The language of the status.

Related

Facebook private replies response doesn't give user_id

I use the Private Replies API call to send a message in Messenger to someone commenting on a Facebook post. If the doc says I should get back the message id and the user_id, I only get the message id in response:
body:
'{"id":"m_ZpLkUdJGAoJ9WCpeROAvWJaHycesSPGVPmoNH0SXTUB8WYqRqHl0ru0y3mniMP3q7YL9rl1lfuQr0x9fyNP
Here is my API call code in ruby:
url = "https://graph.facebook.com/v3.1/" + #comment_id + "/private_replies?message=" + #trigger_message + "&access_token=" + #page_access_token
uri = URI.parse(url)
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
request = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
resp = https.request(request)
Why don't I get the user_id?
Facebook private replies API was supposed to return App scoped ID or ASID, though post the new app verification or around that time, they have stopped giving ASID into private reply API - most probably because they are heading towards returning Page scoped ID or PSID (PSID Migration)
Though you can still get ASID from either :
a. The webhook that could be providing comment ID, it should be providing ASID as sender_id
b. from field with parsing comment ID. This is used to parse single comment node, you can find more info here
Do keep in mind ASID to PSID migration...

Firebase Chat - Notification to Other User

I am currently building an app using Firebase, and decided to implement a chat as well.
I was able to use JSQMessagesVC as a GUI, and get the Firebase chat aspect working as well (by combining 2 UID's to create a chatroom, ex: /123_456). However, I am lost on how to notify the other user if they have received a message. (If user 123 opens chatroom 123_456 and sends a message in it, how do I notify user 456 that they have received a message?)
Thanks for the help!
Your question is more related to designing your database. In case implementing chat functionalities you need to rethink about your database structure again. Its all about database structure as Firebase doesn't provide you any trigger so that you can do some actions on other nodes (i.e. database tables) with your primary action in the node you're in.
Though you might've read all those tutorials already, you can take a look again anyway about structuring your data
Here's a nice chat example which might help you in your case. Though its referring to a group chat. You might take a look at how the database is structured for this purpose.
Basically, you need to put some extra actions from client side in different nodes when someone opens a room to chat with others.
Oh here's another SO Answer you should take a look at.
I had the same issue, which I solved by putting in an additional node where each user has a number of chatrooms. put an observer on the user in the chatroom (in below case "0888a5dc-fe8d-4498-aa69-f9dd1361fe54"), with a counter, a description and a timestamp. each new message, update counter, and lastMessage, etc. see below:
"Messages" : {
"0888a5dc-fe8d-4498-aa69-f9dd1361fe54" : {
"0888a5dc-fe8d-4498-aa69-f9dd1361fe5451879163-8b35-452b-9872-a8cb4c84a6ce" : {
"counter" : 2,
"description" : "Breta",
"lastMessage" : “cool”,
"lastUser" : "51879163-8b35-452b-9872-a8cb4c84a6ce",
"messageType" : "txt",
"sortTimestamp" : -1.459518501758476E9,
"updatedAction" : 1.459518501758468E9,
"userId" : "51879163-8b35-452b-9872-a8cb4c84a6ce"
},
"0888a5dc-fe8d-4498-aa69-f9dd1361fe547bfe8604-58ad-4d18-a528-601b76dd2206" : {
"counter" : 0,
"description" : "Romeo",
"lastMessage" : “yep”,
"lastUser" : "0888a5dc-fe8d-4498-aa69-f9dd1361fe54",
"messageType" : "txt",
"sortTimestamp" : -1.459527387138615E9,
"updatedAction" : 1.459527387138613E9,
"userId" : "7bfe8604-58ad-4d18-a528-601b76dd2206"
}
}

facebook4j search public posts issue

I am using facebook4j to collect posts on a particular topic.
The code runs all find and I am able to search the topics.
But the result also contains the irrelevant data. Example if i search a keyword such as "messi", i am expecting the posts that contains the word "messi". But the result contains the posts of a user whose name is "messi". How can i avoid this problem?
My code is as follows
Facebook facebook = FacebookFactory.getSingleton();
facebook.setOAuthAppId("********", "*****************");
facebook.getOAuthAppAccessToken();
ResponseList<Post> results = facebook.searchPosts("messi");
for (Post result : results) {
System.out.println("************");
System.out.println(result);
}

Retrieve User ID of Facebook App Invitor

In the context of a given Facebook app, suppose User A invited user B to start using it. Once User B accepts to use the app, is there any way to retrieve the ID of User A programmatically (via either PHP/JS SDK) ? This doesn't seem quite documented.
For what it's worth, A/B users are friends, if it's any use.
when user comes following the app request, you can get request id's using
$_GET['request_ids']
then retrieve all the request ids with which you can call graph api to get the corresponding request details like below:
if(isset($_GET['request_ids']))
{
$request_ids = $_GET['request_ids'];
}
$request_ids = explode(",", $request_ids);
foreach($request_ids as $request_id)
{
$request_object = $facebook->api($request_id);
//this $request_object have sender facebook id in the field uid_from
}
If you look here:
http://developers.facebook.com/docs/reference/dialogs/requests/
You can see the object layout. Of note is the data property:
Optional, additional data you may pass for tracking. This will be
stored as part of the request objects created. The maximum length is
255 characters.
In this object you can add your referring UserId and then when the request is claimed, you can then process it on your end.
Hope this helps.

How can I construct a link to view a message on facebook.com if I have the message id

I am retrieving the messaging inbox with the following graph api call:
https://graph.facebook.com/me/inbox?access_token=...
It returns an array of messages that each have an identifying "id" (same as message id as returned by FQL)
I would like to be able to provide a link for the user to view a message directly on Facebook.com.
Something like: https://www.facebook.com/messages/?action=read&tid=....
That is the link you get to if you you browse to a message from facebook itself.
However I can't seem to find a way to discover the correct tid from either the Graph API or FQL.
I haven't had any luck in figuring out an alternative URL either.
This url used to work, but is broken for me now: http://facebook.com/?sk=messages&tid=...
It just redirects to the top level messaging page: https://www.facebook.com/messages/
ANY IDEAS?
Thanks so much
From graph API get to me/inbox the result set gives id as part of each object returned. This is also thread_id.
If the id you have is a string object (probably a guid), this is from Facebook's older message system storage structure. Now they've updated to a new storage structure that requires the old ones to be migrated into the new
So you have a fairly easy check:
If thread id is a long (Int64/BigInt), then you have a new thread and can use
http://www.facebook.com/messages/?action=read&tid=id.THREAD_ID
If thread id is a string then you have a older thread and can use
http://www.facebook.com/messages/?action=read&tid=THREAD_ID
many programming languages have their own form of checking the type of a value.
var threadId = (string)data.thread_id;
var longVal = 0L;
var isLong = Int64.TryParse(threadId, out longVal);
var threadUrl = (isLong) ?
"http://www.facebook.com/messages/?action=read&tid=id." + threadId :
"http://www.facebook.com/messages/?action=read&tid=" + threadId;