我很困惑应该如何实现 gmail API 中的监视功能以接收node.js 脚本内的推送通知。我是否应该在无限循环或其他内容中调用该方法,以便在调用后它不会停止侦听电子邮件通知?
这是我在 node.js 中编写的示例代码:
const getEmailNotification = () => {
return new Promise(async (resolve, reject) => {
try{
let auth = await authenticate();
const gmail = google.gmail({version: 'v1', auth});
await gmail.users.stop({
userId: '<email id>'
});
let watchResponse = await gmail.users.watch({
userId: '<email id>',
labelIds: ['INBOX'],
topicName: 'projects/<projectName>/topics/<topicName>'
})
return resolve(watchResponse);
} catch(err){
return reject(`Some error occurred`);
}
})
Run Code Online (Sandbox Code Playgroud)
谢谢你!