Track conversation chain from different server sessions to one number - php

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.

Related

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. :)

Twilio sms reply feature

I've created a system based on cron job which generates new tasks for user. When new task is generated cron file sends a sms to user using twilio sms api. Now I want make something like when user reply to that sms with word "Done" that task associated with sms will be completed in my database.
I've set reply URL in twilio account. And everything is working fine. My point is how can I complete that task in database. Is there any feature exist in twilio that can help me to send extra parameter as task id, so that I can fetch that id from reply and update the status of task.
Twilio developer evangelist here.
There's no way in SMS to reply to a specific message (try it yourself in your SMS app on your phone), so there's no ID that you can pass around secretly within conversations.
There are a few ways you can work around this.
If the user is sent only one task at a time, then you can record what their current task is and when you receive the message saying "done" tick that one off and send them the next task.
You could send them a task ID within the message, and tell them to include that ID when they respond to say they are done. You could then parse the ID out of the message and mark it as done in your database.
Or, and this might be too far, you could use a pool of numbers to send tasks out such that each live task for a user has a unique number the user responds to. Then you can look up the task based on the user and the number they replied to. Then you can free up the number for a new task.
Let me know if this helps at all.

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.

How shall I setup user event notifications in my code?

I'm coding a team collaboration web app in PHP, and I have a few events that users get notified about through email and/or SMS. The current way I'm doing it is as follows:
Every user has his notification settings in the database as boolean variables.
Say users would be notified when someone comments on the team's page. When the function that posts a comment is called, the same function would contain extra code that checks "who wants to be notified about this?" and then sends notifications to them (which slows down the function a bit).
Is there a more efficient/faster/flexible way to setup notifications? maybe through a script that runs via a cron job? or shall I just keep doing it this way?
I appreciate your help.
I implemented a similar method to the one you're following on a website with a multi-table approach. The users table held the contact information along with opt-in, opt-out options while an event table held the instructions to notify. Several other events were hard coded because of their importance. The thing that set the site apart a bit was a "workflow" area on the user's dashboard that also showed the user what action items they had. We found that most users ignored the emails and dealt directly with that dashboard workflow area. You'd be surprised how many times people change emails or just ignore them altogether.
With 280,000 users and daily visits in the tens of thousands, there was no performance issue noticed. However, the process of queuing emails can be inefficient if you're not careful, so take particular time to benchmark your mail sending functions--its as easy as echoing out microtime before and after the mail send is accomplished--to evaluate its effectiveness. On my current company's site, such improvements yielded a 800% reduction of email queuing time (queuing being the process of generating the emails and submitting them via php mailer to the mail system for distribution)
I'd say have a table that is a queue of notifications. Let the function that post a comment still check "who wants to be notified about this?" but then just log entries containing the messages in this table. Then have a separate process work from the queue i.e. your cron job suggestion.
Depending on your database you may perhaps make use of database events or triggers instead of a cron job. This however have the requirement that your database allow you to put code in your database that will send the SMS or Email. This poses a security risk normally which you may or may not be concerned about in your setup.

Processing input data from a phone though SMS

I currently have a php script that reads email messages sent from phones and then it posts them on the internet...
I want to do the same thing but instead of reading emails, I would like to process the data through a SMS.
so, if someone sends a sms to X number, my program would get the message through this X number and use it in the site.
What would I need?
You have a lot of options here. Generally speaking you need to first acquire the number that people can SMS to. Afterwards a callback will be issued to you when someone sends and SMS and your PHP script can process the message and figure out what to do with it.
You essentially have a few options for exposing the SMS
Go with a dedicated short-code. Eg someone sends a text to 53234 and you will get a callback to deal with it. This is an expensive option. It will cost you generally around $2500/mo minimum not including per messaging fees. You can requisition a short code through a provider such as Open Market. You have to get approval for the short code which might take as much as 6 weeks.
Go with a shared short code. This is a bit cheaper -- you pay per message and it usually costs anywhere from .02 - .10 /msg depending on your msg volume. You get a keyword such as DOLLY that people have to preface before texting. So they would text DOLLY INPUT TEXT HERE to 53234 now and you would receive the callback with INPUT TEXT HERE. Celltrust and others provide shared short codes.
Use a public api that allows people to text in through something such as TextMarks. Textmarks is like a shared short code requires keyword usage + users to subscribe. However it is free which is great and has a nice callback API. I believe a service called Mozes also provides similar service. Finally, if you want you can set up a Twitter account and have people text to their number. Facebook also a similar API that allows you to use their FBOOK shortcode.
Ish's answer was great. I'd like to point out an additional free option.
Most carriers allow you to send SMS messages to email addresses. The SMS text gets inserted into the body of the email and the subject is carrier dependent. Once the email is sitting in your inbox, you can access it by the method of your choice.
This method has worked for me in a personal project requiring remote communication with my computer when all I had was a cell phone without internet capabilities.
This Wikipedia page shows how many carries support this (practically all).
Just try not to violate anyone's terms of service, you could always host the email account yourself.

Categories