`
sillycat
  • 浏览: 2560098 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Mail Solution with Content.IO

 
阅读更多
Mail Solution with Content.IO

1. Introduction
The develop console is here
https://console.context.io

The API key and secret are here
https://console.context.io/#settings/keys

Lite API will be more powerful for web hook. 2.0 API will be more powerful to filter lists of historical messages.

2. Start from 2.0 API
Here is the clients.
https://context.io/docs/2.0/libraries

I directly go to there console and try the “explore”. This should work for me.
subject = /sillycat post ([0-9]*): (.*)$/
email = gmail.com
filename = /(.*?)\.(pdf|doc|docx|DOC|DOCX|PDF)$/

I will go on trying the nodeJS client library.
https://github.com/contextio/ContextIO-node

Based on the nodeJS API, here are some codes
    fetchAccountInfoByEmail : function(email, callback) {
        var logger = services.get('logger');
        var cxtioClient = this.cxtioClient;

        logger.trace("EmailScan - fetchAccountInfoByEmail - email = " + email);
        cxtioClient.accounts().get({ "email": email}, function(err, data){
            if(err){
                logger.error("EmailScan - fetchAccountInfoByEmail - fail to fetch the account info.");
                callback(err, null);
                return ;
            }
            if(data){
                logger.trace("EmailScan - fetchAccountInfoByEmail - get the account info with key = " + email);
                logger.trace("EmailScan - fetchAccountInfoByEmail - get response info = " + JSON.stringify(data));
                var msgBody = data.body;
                logger.trace("EmailScan - fetchAccountInfoByEmail - get message body = " + JSON.stringify(msgBody));
                if(msgBody.length > 0){
                    var account = _.first(msgBody);
                    logger.trace("EmailScan - fetchAccountInfoByEmail - get account = " + JSON.stringify(account));
                    callback(null, { id: account.id } );
                    return ;
                }else{
                    logger.trace("EmailScan - fetchAccountInfoByEmail - fail to fetch any account info with key = " + email);
                    callback(null,null);
                    return ;
                }
            }else{
                logger.trace("EmailScan - fetchAccountInfoByEmail - fail to fetch any account info with key = " + email);
                callback(null,null);
                return ;
            }
        });
    }

Scan the emails with some filters
    fetchSillycatJobsByAccountId : function(accountId, pager, callback) {
        var sillycatFilter = {
                        subject: ‘/sillycat post ([0-9]*): (.*)$/',
                        from: ‘robot@sillycat.ddns.org'
                    } ;
        var filter = _.merge(sillycatFilter, pager);

        this.fetchEmailsWithFilter(accountId, filter, callback);
    }

Scan the emails with attachment
    fetchSillycatResumesByAccountId : function(accountId, pager, callback) {
        var sillycatFilter = {
                        file_name: '/(.*?)\.(pdf|doc|docx|DOC|DOCX|PDF)$/',
                        email: ’sillycat.org'
                    } ;
        var filter = _.merge(sillycatFilter, pager);

        this.fetchEmailsWithFilter(accountId, filter, callback);
    },

Private Method provide here for previous methods.

    fetchEmailsWithFilter : function(accountId, filter, callback){
        var logger = services.get('logger');
        var cxtioClient = this.cxtioClient;

        logger.trace("EmailScan - fetchEmailsWithFilter - accountId = " + accountId);
        logger.trace("EmailScan - fetchEmailsWithFilter - filter = " + JSON.stringify(filter));

        cxtioClient.accounts(accountId).messages().get(filter, function(err, data){
            if(err){
                logger.error("EmailScan - fetchEmailsWithFilter - fail to fetch the messages.");
                callback(err, null);
                return ;
            }
            if(data){
                logger.trace("EmailScan - fetchEmailsWithFilter - get the messages info with filter = " + JSON.stringify(filter));
                logger.trace("EmailScan - fetchEmailsWithFilter - get response info = " + JSON.stringify(data));
                var msgBody = data.body;
                logger.trace("EmailScan - fetchEmailsWithFilter - get message body = " + JSON.stringify(msgBody));
                if(msgBody.length > 0 ){
                    callback(null, msgBody);
                    return ;
                }else{
                    logger.warn("EmailScan - fetchEmailsWithFilter - fail to fetch any messages with filter = " + JSON.stringify(filter));
                    return ;
                }
            }else{
                logger.warn("EmailScan - fetchEmailsWithFilter - fail to fetch any messages with filter = " + JSON.stringify(filter));
                return ;
            }
        });
    }

Fetch the Email Content
    fetchMailContent: function(accountId, messageId, callback) {
        var logger = services.get('logger');
        var cxtioClient = this.cxtioClient;

        logger.trace("EmailScan - fetchMailContent - accountId = " + accountId);
        logger.trace("EmailScan - fetchMailContent - messageId = " + messageId);

        cxtioClient.accounts(accountId).messages(messageId).body().get(function(err, data){
            if(err){
                logger.error("EmailScan - fetchMailContent - fail to fetch the mail content.");
                callback(err, null);
                return ;
            }
            if(data){
                logger.trace("EmailScan - fetchMailContent - get response info = " + JSON.stringify(data));
                var msgBody = data.body;
                logger.trace("EmailScan - fetchMailContent - get message body = " + JSON.stringify(msgBody));
                if(msgBody.length > 0){
                    var contents = _.filter(msgBody, function(item){
                        return (item.type==='text/plain');
                    });
                    logger.trace("EmailScan - fetchMailContent - get any plain result = " + JSON.stringify(contents));
                    if(contents.length > 0){
                        logger.trace("EmailScan - fetchMailContent - return the text content.");
                        callback(null, _.first(contents));
                        return ;
                    }else{
                        logger.trace("EmailScan - fetchMailContent - return the html content or etc.");
                        callback(null,_.first(msgBody));
                        return ;
                    }
                }
            }else{
                logger.warn("EmailScan - fetchMailContent - fail to fetch any email content.");
                return ;
            }
        });
    },

Fetch the Attachment Link
    fetchAttachmentLink: function(accountId, fileId, callback){
        var logger = services.get('logger');
        var cxtioClient = this.cxtioClient;

        logger.trace("EmailScan - fetchAttachmentLink - accountId = " + accountId);
        logger.trace("EmailScan - fetchAttachmentLink - fileId = " + fileId);

        cxtioClient.accounts(accountId).files(fileId).content().get({ as_link: 1 },function(err, data){
            if(err){
                logger.error("EmailScan - fetchAttachmentLink - fail to fetch the file link.");
                callback(err, null);
                return ;
            }
            if(data){
                logger.trace("EmailScan - fetchAttachmentLink - get response info = " + JSON.stringify(data));
                var link = data.body;
                logger.trace("EmailScan - fetchAttachmentLink - get link = " + link);
                callback(null, link);
                return ;
            }
        });
    },

References:
https://www.context.io/
https://context.io/docs/lite/quickstart
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics