How to get twitter's direct message conversation threads in PHP - php

I am striving hard to get the threaded conversation list of twitters direct messages. The twitter seems not providing its conversation API publically.
The work around , I was thinking on , is as below:
There are two separate API:
GET direct_messages : This api returns all the direct messages sent
to you.
GET direct_messages/sent : This api returns all the direct
messages sent by you.
I am planning to compare/merge the list based on the sender and receiver id to group them and create conversation threads.
But it is not feasible to mix these two result to get the entire conversation.Because we get 20 to 30 messages per call it might be possible that the entire conversation might not match.
Can anyone suggest or route me to a more precise solution which will provide me with the direct message conversation thread

From the documentation:
You can request up to 200 direct messages per call, and only the most recent 200 DMs will be available using this endpoint.
Really, the best you can do is take the 200 most recent messages sent to your user. Then, you can grab up to 800 messages sent by the user.
You'll have to thread them yourself based on their timestamp.
There's no other way to do this at the moment.

Related

Plivo SMS debugging - Get all logs for specific message

I am trying to figure out how to be able to show the user a list of delivery logs related to the specific message they sent out. Right now I can't seem to find a way of grouping the logs, short of basing it off of a date and time and maybe anything within 5 minutes of the time it was sent. This is because it assigns a different message and request ID to each number that message was sent to. But is there a better way?
Go to Plivo > SMS > Logs
Download logs
Open with Notepad++
???
Filter as you please.
Otherwise, check API documentation.
The Plivo webapp offers these filters: "From/To/Direction/Status/Error/Timeframe" as per: snag.gy/P6sRNH.jpg
Also, if you mean to display to the user programatically through REST API, I think you should make an app with the following flow:
auth api
fetch all logs (maybe pass a value of last time they sent messages + 14 days... if that is saved somewhere) > URI: /v1/Account/YOURACCOUNTMX3$cD../Message/
Search by Params : { text : Your Text Here

Gmail API Missing Messages

I am having trouble retrieving all messages through the Gmail API PHP Library. I use listUsersThreads to retrieve all threads to do either a full or partial mailbox sync on a user's account. The initial full sync processes and returns the message ids I need, which I then use to store mail meta headers (from, to, date, subject). A subsequent call using listUserHistory from the last history id, allows me to do a partial sync to only retrieve the latest messages. From the data I have stored, I then display a full message conversation log between two parties, ordered by date to show the conversation. Clicking the message will then query the API to retrieve email body, which I then display.
The issue is that based on the messages I have saved, looking at the MIME content, there are messages in the MIME body, that I do not have on my database. I then also tried to query the API using a search query, and still there are missing messages, not returned by the API.
A previous developer used mimecast to get the messages, and querying that database does in fact return the messages that I'm missing.
How is the Google Gmail API not giving me all messages between sender and receiver? The MIME body clearly shows messages that are not available when querying the API, and I don't understand why, or how to find the missing messages.
Any assistance would be appreciated.
So, for in case this issue comes up for anyone else, I believe that it has something to do with expired history items. I stand to be corrected, as this can only be proved after I've had my implementation running for more than two weeks.
If you're considering running a mailbox sync, there's a good chance that you'll be missing messages, especially if those messages were sent from a client other than Inbox or Gmail. History items are kept for two weeks on average, so by syncing a mailbox, you'll be syncing everything from when the account was activated, but expired history items will not be available.
In theory, this means that you should have the full email conversation while a partial sync is executed. You should have all the MIME headers that you need as and when the communication takes place, provided that, like me, you have push notifications enabled through the Google Cloud Services console to alert your systems to run a partial sync process on any given account.
If your partial sync is executed manually, or possibly through CRON on an interval, rather than through push notifications, you'll need to make sure that the interval is configured to sync while all history items are still available and accessible.
The downside of this, though, is that even if you do have trace of all communication with their message IDs, a lookup on an expired message to retrieve email body will fail with a 404 status code, and you will not be able to retrieve email body contents for some messages.
Thus, if your processes relies heavily on what's in the body of the email, you should also store body content locally during a partial sync I really only need the MIME headers, although I do lookup message contents when needed, but it won't cause major problems for me if I was unable to retrieve the body of any given message.
I should be able to confirm this theory within a month from now, so if you think my theory is incorrect, please feel free to make me the wiser. :)

Track conversation chain from different server sessions to one number

I'm creating an app which will allow all users to initiate conversation with page owner using Chat-to-SMS service.
Problem is that I'm not sure how to track conversations since there would be one conversation chain from SMS Provider <-> page owner. When owner clicks reply, it needs to know which session user it needs to send the message to.
Basically, I need to do opposite what is being explained here: https://www.twilio.com/docs/quickstart/php/sms/tracking-conversations
How should I accomplish this? I can't quite wrap my head around this.
Twilio developer evangelist here.
If you are sending all the chat messages to just one SMS number then there is no easy way to tie replies back to the original message. (As a quick experiment, if you open an SMS conversation in your own phone and try to reply to any message that wasn't the latest one, you'll see it's not possible.)
There are workarounds though.
You could, when forwarding the message, generate an ID for it. Then get your page owner to include that ID when responding to that message, that way you can route the message back to the original sender and strip out the ID.
Alternatively, when replying you could always respond to the last message that came in. This relies on there not being much traffic, allowing the page owner to respond before the next message arrives. This is error prone though.
Another alternative is purchasing a new number for each new conversation. You could expire the number after a predetermined amount of time. This is made much easier with Twilio Proxy, which was announced recently and is currently in preview.
Let me know if that helps at all.

Telegram BotApi, Send message to multiple chat_id

I was wondering if I could send a message with my bot on telegram bot api, to multiple chat_id, but I cant figure it out. that's totally because of telegram apis are so hard to understand.
I have used this for send a message to one chat_id:
https://api.telegram.org/botTOKKEN/sendMessage?chat_id=xxxxxxx&text=Hi+John
There is no way to make bot to sendMessage to multiple chat id but there is a trick that can fix it for now :)
Why not sending each chat id a message ?!
Let's look at this example in PHP :
<?php
$message = "Hi John";
$chatIds = array("xxx","xxx","xxx"); // AND SOME MORE
foreach($chatIds as $chatId) {
// Send Message To chat id
file_get_contents("https://api.telegram.org/botTOKKEN/sendMessage?chat_id=$chatId&text=".$message);
}
?>
In addition to #farsad 's answer: Add sleep(NUMBER_OF_SECONDS); inside the foreach loop to not get banned by telegram. As there is a limit of 30 messages per second for bots in Telegram API
The problem with foreach or any other massive sendMessage is that the API will not allow more than ~30 messages to different users per second.
According to Bots FAQ in telegram site:
How can I message all of my bot's subscribers at once?
Unfortunately, at this moment we don't have methods for sending bulk messages, e.g. notifications. We may add something along these lines in the future.
In order to avoid hitting our limits when sending out mass notifications, consider spreading them over longer intervals, e.g. 8-12 hours. The API will not allow more than ~30 messages to different users per second, if you go over that, you'll start getting 429 errors.
You can't send message this way to all user.
and solution in Bots FAQ page:
My bot is hitting limits, how do I avoid this?
When sending messages inside a particular chat, avoid sending more than one message per second. We may allow short bursts that go over this limit, but eventually you'll begin receiving 429 errors.
If you're sending bulk notifications to multiple users, the API will not allow more than 30 messages per second or so. Consider spreading out notifications over large intervals of 8—12 hours for best results.
Also note that your bot will not be able to send more than 20 messages per minute to the same group.
Just for your information.
We could input the chat_ids to database. Query and loop the message section for sending the message to multiple chat-id with sleep().
I am not a programmer. So I could not make an example.

Get all SMS from a particular number in inbox - Esendex

I am using the Esendex Rest API http://developers.esendex.com/APIs/REST-API/inbox and I am trying to get all the messages in the inbox from a particular phone number.
This is basically so if I do a send out of SMS's and someone replies I am able to retrieve the reply.
I can currently get all the messages in the inbox into an array and sort them, but this is too slow with over 4000 messages.
Does anyone know if I can filter by phone number, or at least date-received in my API requests? Even if I could get the last weeks worth of SMS's.
You can try to use the conversation API if you want to filter by a phone number. The drawback is that you can get only the latest 15 messages, also that there is no reliable way to detect if a reply has been received from a phone number other than the inbox API. Apart from this I wasn't able to find anything that can help you. Their API is very restrictive and minimal.
There's currently no way to do this via the REST API but I'll see if we can add it over the next couple of weeks (I work for Esendex). As you note the functionality already exists in the Conversations API albeit in a more minimal form.
In the meantime you could achieve this by checking all messages that arrive either by polling the Inbox or using Push Notifications.

Categories