<commit_before>/*
    Handle facebook messenger API
*/

const request = require('request');
const crypto = require('crypto');
const line = require('@line/bot-sdk');
const fs = require('fs');
const URL = require('url');
const easyimg = require('easyimage');
const mime = require('mime-types');
const Thumbler = require('thumbler');
const mm = require('music-metadata');

const Const = require('../../constants');
const BaseAdapter = require('./BaseAdapter');

class LineAdapter extends BaseAdapter {

    constructor(config) {
        super(config);

        this.provider = Const.line;
    }

    processGet(req,res,service,cb){

        return false;

    }

    processPost(req,res,service,cb){

        const data = req.body;
        const signatureOrig = req.headers['x-line-signature'];

        console.log('line event',data);
        
        if(!signatureOrig)
            return false;

        if(!data.events)
            return false;

        if(data.events.length < 1)
            return false;

        const channelSecret = service.config.channelSecret;
        const body = req.rawBody; // Request body string

        const signatureGenerated = crypto.createHmac('SHA256', channelSecret).update(body).digest('base64');
        
        // process if signature is matched
        if(signatureGenerated == signatureOrig){

            this.logger.debug('Line Webhook');

            // Iterate over each entry - there may be multiple if batched
            data.events.forEach((event) => {

                if(event.type == 'message'){
                    
                    if(cb){
                        cb(
                            Const.eventTypeMessage,
                            this.generateMessageObj(service,event),
                            event
                        );
                    }

                }

            });
            
            res.send('ok');

            return true;

        } else {
            
            this.logger.warn('Line wrong signature ' + signatureOrig + ' expected ' + signatureGenerated);

        }
        
        
        return false;

    }
<commit_msg>Add line newchat event support<commit_after>/*
    Handle facebook messenger API
*/

const request = require('request');
const crypto = require('crypto');
const line = require('@line/bot-sdk');
const fs = require('fs');
const URL = require('url');
const easyimg = require('easyimage');
const mime = require('mime-types');
const Thumbler = require('thumbler');
const mm = require('music-metadata');

const Const = require('../../constants');
const BaseAdapter = require('./BaseAdapter');

class LineAdapter extends BaseAdapter {

    constructor(config) {
        super(config);

        this.provider = Const.line;
    }

    processGet(req,res,service,cb){

        return false;

    }

    processPost(req,res,service,cb){

        const data = req.body;
        const signatureOrig = req.headers['x-line-signature'];

        console.log('line event',data);
        
        if(!signatureOrig)
            return false;

        if(!data.events)
            return false;

        if(data.events.length < 1)
            return false;

        const channelSecret = service.config.channelSecret;
        const body = req.rawBody; // Request body string

        const signatureGenerated = crypto.createHmac('SHA256', channelSecret).update(body).digest('base64');
        
        // process if signature is matched
        if(signatureGenerated == signatureOrig){

            this.logger.debug('Line Webhook');

            // Iterate over each entry - there may be multiple if batched
            data.events.forEach((event) => {

                if(event.type == 'message'){
                    
                    if(cb){
                        cb(
                            Const.eventTypeMessage,
                            this.generateMessageObj(service,event),
                            event
                        );
                    }

                }


                else if(event.type == 'follow'){
                    
                    if(cb){
                        cb(
                            Const.eventTypeNewChat,
                            this.generateOpenChatObj(service,event),
                            data
                        );
                    }

                }

            });
            
            res.send('ok');

            return true;

        } else {
            
            this.logger.warn('Line wrong signature ' + signatureOrig + ' expected ' + signatureGenerated);

        }
        
        
        return false;

    }

    generateOpenChatObj(service,event){

        const messageObj = {
            userIdentifier:service.provider + ":" + service.identifier + ":" + event.source.userId,
            providerIdentifier:service.identifier,
            provider:service.provider,
            providerUserId:event.source.userId,
        };

        return messageObj;

    }

{'fraction_non_alphanumeric': 0.11002396166134186, 'fraction_numerical': 0.0023961661341853034, 'mean_word_length': 1.5362025316455696, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4907801', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1187, 'n_words': 263}