gmail API Users.messages: list is not getting replies - google-api

If I make a request to the gmail API for Users.messages.list it will return 100 of the newest messages.
If I make another request, 1 minute later and there are no new emails it will return the same 100 most recent messages.
If I make a third request, 2 minutes after the original and there is a reply to one of the 100 most recent messages it will still return the 100 most recent messages.
The problem with this is the it only returns the message id and thread id, not if there is a new reply or not. That would mean that I would have to check every message that I have locally stored, or every one of the 100 returned messages just to know if there was a reply to it or not.
The way that works, you couldn't "check" your email via the api because if you had stored 10000 messages and you were checking replies on all of them you would use up your entire API "number of requests" allocation in a single day!
What's wrong with you Google?
Sure I could use pop3 or imap but why when I could just use something like /list_recent?

You should definitely take a look at this part of the Gmail API documentation: https://developers.google.com/gmail/api/guides/push
Basically, it explains you how you can setup a "watch" over a Gmail account and receive notifications (new e-mail, deleted e-mail, labels added...) through Pub/Sub, the messaging queue protocol from Google.
The notifications will contain an historyId, which is a kind of milestone in the Gmail account. Using the /history endpoint, you'll then get the e-mails that were added and/or deleted since this history point. You have to store the latest historyId you handled somewhere in your app, so that you can query the right changes (and not miss anything) on the next notification.
If you don't need to react to changes in real-time, maybe you can call the /history endpoint periodically, but it will definitely be less efficient at scale.
It takes a bit of work to get this working but at the end of the day you get a very efficient system able to react about changes in real-time.

Related

How to improve email robustness? [mailgun, laravel]

I planned to improve email robustness using below way. I am using laravel and mailgun. If any bounces email is in the mailgun then i want to send that to someone related to the email(not to that previous receiver and it related with previous sender or someone) so that i plan to use Task scheduler and schedule every hour this process. After that email send successfully i want to remove that email from mailgun.
I actually want to know how to implement get bounces email from mailgun and if send successfully using Task scheduler i want to remove that email from mailgun. if any sample's to achieve this? or any different ideas?
Normally when a bounce happens, providers such as Mailgun will retry a few times depending on the bounce status/type. After failing to send to that email address, Mailgun will put that address into your Bounces list to avoid sending to that address in the future. There's really no point in trying to send emails to an address which doesn't exist, inbox is full, domain expired, etc. So perhaps you should review your bounce list before you consider doing this. Mailgun's dashboard provides the description and date it happened. If you wish to send to a particular address again, you can remove the address from the bounce list. More about bounces here.
You can use mailgun-php or simply use Guzzle to call their API to retrieve your bounces. I'm not sure what you meant my removing email, but hope this helps.
Edit:
You can use their API to get all your bounces, and also the message content (retention depends on your plan or settings) if you need the original content. You could also use their webhooks to get the failed emails, so you don't really need to schedule/cron.
Edit 2:
The bounce list only stores email addresses of previously bounced emails. Also, I just realized that, if you're checking hourly for bounced messages, looks like the suppression/bounce API won't be of much help since you can't know if a message has failed for an address already on the list. This is the only data from bounce list:
{
"address": "alice#example.com",
"code": "550",
"error": "No such mailbox",
"created_at": "Fri, 21 Oct 2011 11:02:55 GMT"
},
So looks like you've to use the events API or webhooks to get the failed messages.
I've used mailgun-php sometime ago, and I found that the it wasn't easy working with Events on that library due to protected properties of the class. So if you have trouble, maybe just use Guzzle or use webhook approach instead.

slack chat.postMessage vs. incoming webhook?

I am trying to send messages from several outer sources to a specific channel, which is private and belongs to myself only. The username should be the name of source, not my ID.
I found there are two ways to do such a similar function: Incoming Webhooks and chat.postMessage
I have already practiced these two, which seems no difference between them.
However, in Incoming Webhooks, a statement says:
You can't use Incoming Webhooks with Workspace Apps right now; those
apps can request single channel write access and then use
chat.postMessage in the Web API to post messages, providing very
similar functionality to Incoming Webhooks.
What does it mean?
To my work, which one is better?
with chat.postMessage() you send a message to a specific channel, often you do that in response to a users action. You will need the token to verify the postMessage Request which you receive when the user installs your app.
Incoming webhooks are often used to post general information, e.g. patch notes or general announcements.
As far as I know, you don't need the token since there is a verification behind that Url.
so the webhook url is bound to a specific channel, which is specified through the user. With chat.postMessage you can post messages anywhere (depending on your permissions, maybe not in private channels or direct messages)
Adding to what Ben said:
Incoming webhooks are limited in their functionality. They are great if you need an easy way to send a message that does not require a token, but in general the API method (chat.postMessage) is the better choice. It is more flexible (e.g. not fixed to one channel) and provides the full functionality (e.g. you get the ID for a message and can later update it).
Workspace apps / tokens where a new functionality that allowed apps to be installed in one channel only (among other things). It never left its beta stage and can be safely ignore for further development.

Gmail API push notification duplicate message ids

I am using Gmail push notifications for email processing. When I receive a message notification from Google, I use the history list API with the previously stored historyId and can see new messages using messagesAdded. Once I have retrieved the messages, I store the last historyId for the next request.
When testing, if I send two mail messages (about 30 seconds apart) to the watched mailbox, I see two messages notifications from Google and when I call the history API for each of the notifications I see the message ids. This is all good.
If I repeat the same test but send the two mail messages immediately after each other, I get the two notifications from Google but when I call the history API for the first message, I get the two message ids in that history API call. When I call the history API call for the second notification, I get a duplicate of the last message id.
How can I prevent seeing duplicate message ids in this scenario? Any help or pointers would be much appreciated.
Updated: Added request and response example for second test.
I noticed in the response from the Google History API a historyId showing the latest messages that have been processed. If I store this value as the next start point instead of the historyId sent in the notification message, it works around the duplicate issue as on the second request there are no more messages. Not sure if this is the right way to do this as it means I don't store or use the historyId sent in the notification message.

Unable to get timestamp of the message posted by Slack App

Whenever my app posts ephemeral message to Slack channel (in response to a query by a user), I am unable to get the timestamp of my Slack app response. As I want to delete it once the user has made a selection using one of the buttons. Although I have subscribed to 'message.channels' event, I don't get a notification to my app whenever my app posts in the channel (in response to the user input), therefore, I am unable to get the timestamp of the message which I'll use to delete it. All I want is the timestamp of the message posted by my app so that I can delete it but I am unable to receive the timestamp. Please help!
For e.g. in Giphy app for Slack. Let's say the user invokes the app by calling '/giphy [dog]' where 'dog' is just an example of a search term. The app responds by sending a gif and user can either send it, shuffle to the next one or cancel it. I want a similar capability of cancelling the app response but I need the timestamp of the message in order to do so therefore I am asking for help.
Thanks.
Your approach can not work, because Slack is handling ephemeral messages differently from "normal" messages. They are only visible by one user and can not be modified by API methods (e.g. deletion).
But of course its possible to replace ephemeral messages. Here is how:
Your app can just reply to the interactive message request from Slack with a new message. That new message will by default override the original message including ephemeral messages.
You can reply in two ways:
Directly reply to the request from Slack with a message within 3 seconds
Send a message to the response_url from the Slack request within 30 minutes.
See here for the official documentation on how to respond to interactive messages.
This approach works both with interactive messages and slash commands.
See also this answer for a similar situation.

Is it posible to send multiple message to Google Home through DialogFlow?

On facebook Chatbot or others platform we can send message directly to the user.
So for one question we can send multiple answer.
Now, that i'm developing for Google Home, I need to do the same.
I didn't find this opttion.
Dialogflow HTTP call to my server is the only output available.
So is there anything i missed to send message back to user in case I have a multiple messages answer?
(Or do I have to bufferise very message my hook creates before sending it back?)
Thanks
The conversation model for the Google Assistant is different - you can only send a message to the user in response to the user sending you a message. You can only send a single response, but it may have multiple parts (up to two Simple responses, containing messages, plus other features such as cards and carousels).
If you need to send multiple things back - you may need to rethink how you're doing it or how much you're sending back at a time. The Assistant is primarily for audible responses, and a long audible response is generally not a good UX.

Resources