<commit_before>var Telegram = require('node-telegram-bot-api');
var fs = require('fs');
var irc = require('./irc');

// tries to read chat ids from a file
var readChatIds = function(arr) {
    console.log('\n');
    console.log('NOTE!');
    console.log('=====');

    var idMissing = false;
    try {
        var json = JSON.parse(fs.readFileSync(process.env.HOME + '/.teleirc/chat_ids'));
        for (var i = 0; i < arr.length; i++) {
            var key = arr[i].tgGroup;
            if (key in json) {
                arr[i].tgChatId = json[key];
                console.log('id found for:', key, ':', json[key]);
            } else {
                console.log('id not found:', key);
                idMissing = true;
            }
        }
    } catch (e) {
        console.log('~/.teleirc/chat_ids file not found!');
        idMissing = true;
    }

    if (idMissing) {
        console.log(
            '\nPlease add your Telegram bot to a Telegram group and have' +
            '\nsomeone send a message to that group.' +
            '\nteleirc will then automatically store your group chat_id.');
    }

    console.log('\n');
};

var writeChatIds = function(config) {
    var json = {};
    for (var i = 0; i < config.channels.length; i++) {
        if (config.channels[i].tgChatId) {
            json[config.channels[i].tgGroup] = config.channels[i].tgChatId;
        }
    }
    json = JSON.stringify(json);
    fs.writeFile(process.env.HOME + '/.teleirc/chat_ids', json, function(err) {
        if (err) {
            console.log('error while storing chat ID:');
            console.log(err);
        } else {
            console.log('successfully stored chat ID in ~/.teleirc/chat_ids');
        }
    });
};

var getName = function(user) {
    var name = '';

    if (config.ircTgShowName == 1) {
        // first_name
        return user.first_name ? user.first_name + '' : '';
    } else {
        if (config.ircTgShowName == 2) {
            // first_name last_name
            name = user.first_name ? user.first_name + ' ' : '';
            return name + user.last_name ? user.last_name : '';
        } else {
            if (config.ircTgShowName == 3) {
                // first_name last_name @username
                name = user.first_name ? user.first_name + ' ' : '';
                name += user.last_name ? user.last_name : '';
                return name + user.username ? '(@' + user.username + ')' : '(No username)';
            } else {
                // @username
                if (user.username === undefined) {
                    return user.first_name + ' ' + user.last_name;
                } else {
                    return '@' + user.username;
                }
            }
        }
    }
};

module.exports = function(config, sendTo) {
    var tg = new Telegram(config.tgToken,{polling: true});

    readChatIds(config.channels);

    tg.on('message', function(msg) {
        var channel = config.channels.filter(function(channel) {
            return channel.tgGroup === msg.chat.title;
        })[0];

        if (!channel) {
            return;
        }

        if (!channel.tgChatId) {
            console.log('storing chat ID: ' + msg.chat.id);
            channel.tgChatId = msg.chat.id;
            writeChatIds(config);
        }

        var name = getName(msg.from);

        // skip posts containing media if it's configured off
        if ((msg.audio || msg.document || msg.photo || msg.sticker || msg.video ||
            msg.voice || msg.contact || msg.location) && !config.showMedia) {
            return;
        }

        var text;

        if (msg.reply_to_message && msg.text) {
            text = '@' + msg.reply_to_message.from.username + ', ' + msg.text;
        } else if (msg.audio) {
            text = '(Audio)';
        } else if (msg.document) {
            text = '(Document)';
        } else if (msg.photo) {
            var fileid = msg.photo[msg.photo.length - 1].file_id;
            var promise = tg.getFileLink(fileid);
            promise.then(function(path) {
                console.log(path);
                sendTo.irc(channel.ircChan, '<' + name + '>: ' + path);
                return path;
            });
            return;

        } else if (msg.sticker) {
            text = '(Sticker)';
        } else if (msg.video) {
            text = '(Video, ' + msg.video.duration + 's)';
        } else if (msg.voice) {
            text = '(Voice, ' + msg.audio.duration + 's)';
        } else if (msg.contact) {
            text = '(Contact, ' + '"' + msg.contact.first_name + ' ' +
                                        msg.contact.last_name + '", ' +
                                        msg.contact.phone_number + ')';
        } else if (msg.location) {
            text = '(Location, lon: ' + msg.location.longitude +
                            ', lat: ' + msg.location.latitude + ')';
        } else if (msg.new_chat_participant) {
            text = '(Added: ' + msg.new_chat_participant.first_name + ')';
        } else if (msg.left_chat_participant) {
            text = '(Removed: ' + msg.left_chat_participant.first_name + ')';
        } else {
            text = msg.text;
        }

        sendTo.irc(channel.ircChan, '<' + name + '>: ' + text);<commit_msg>use sendTo.irc directly, vary format in some cases<commit_after>var Telegram = require('node-telegram-bot-api');
var fs = require('fs');
var irc = require('./irc');

// tries to read chat ids from a file
var readChatIds = function(arr) {
    console.log('\n');
    console.log('NOTE!');
    console.log('=====');

    var idMissing = false;
    try {
        var json = JSON.parse(fs.readFileSync(process.env.HOME + '/.teleirc/chat_ids'));
        for (var i = 0; i < arr.length; i++) {
            var key = arr[i].tgGroup;
            if (key in json) {
                arr[i].tgChatId = json[key];
                console.log('id found for:', key, ':', json[key]);
            } else {
                console.log('id not found:', key);
                idMissing = true;
            }
        }
    } catch (e) {
        console.log('~/.teleirc/chat_ids file not found!');
        idMissing = true;
    }

    if (idMissing) {
        console.log(
            '\nPlease add your Telegram bot to a Telegram group and have' +
            '\nsomeone send a message to that group.' +
            '\nteleirc will then automatically store your group chat_id.');
    }

    console.log('\n');
};

var writeChatIds = function(config) {
    var json = {};
    for (var i = 0; i < config.channels.length; i++) {
        if (config.channels[i].tgChatId) {
            json[config.channels[i].tgGroup] = config.channels[i].tgChatId;
        }
    }
    json = JSON.stringify(json);
    fs.writeFile(process.env.HOME + '/.teleirc/chat_ids', json, function(err) {
        if (err) {
            console.log('error while storing chat ID:');
            console.log(err);
        } else {
            console.log('successfully stored chat ID in ~/.teleirc/chat_ids');
        }
    });
};

var getName = function(user) {
    var name = '';

    if (config.ircTgShowName == 1) {
        // first_name
        return user.first_name ? user.first_name + '' : '';
    } else {
        if (config.ircTgShowName == 2) {
            // first_name last_name
            name = user.first_name ? user.first_name + ' ' : '';
            return name + user.last_name ? user.last_name : '';
        } else {
            if (config.ircTgShowName == 3) {
                // first_name last_name @username
                name = user.first_name ? user.first_name + ' ' : '';
                name += user.last_name ? user.last_name : '';
                return name + user.username ? '(@' + user.username + ')' : '(No username)';
            } else {
                // @username
                if (user.username === undefined) {
                    return user.first_name + ' ' + user.last_name;
                } else {
                    return '@' + user.username;
                }
            }
        }
    }
};

module.exports = function(config, sendTo) {
    var tg = new Telegram(config.tgToken,{polling: true});

    readChatIds(config.channels);

    tg.on('message', function(msg) {
        var channel = config.channels.filter(function(channel) {
            return channel.tgGroup === msg.chat.title;
        })[0];

        if (!channel) {
            return;
        }

        if (!channel.tgChatId) {
            console.log('storing chat ID: ' + msg.chat.id);
            channel.tgChatId = msg.chat.id;
            writeChatIds(config);
        }

        // skip posts containing media if it's configured off
        if ((msg.audio || msg.document || msg.photo || msg.sticker || msg.video ||
            msg.voice || msg.contact || msg.location) && !config.showMedia) {
            return;
        }

        if (msg.reply_to_message && msg.text) {
            sendTo.irc(channel.ircChan, '<' + getName(msg.from) + '>: ' +
                '@' + getName(msg.reply_to_message.from) + ', ' + msg.text);
        } else if (msg.audio) {
            sendTo.irc(channel.ircChan, '<' + getName(msg.from) + '>: ' +
                '(Audio)');
        } else if (msg.document) {
            sendTo.irc(channel.ircChan, '<' + getName(msg.from) + '>: ' +
                '(Document)');
        } else if (msg.photo) {
            var fileid = msg.photo[msg.photo.length - 1].file_id;
            tg.getFileLink(fileid).then(function(path) {
                sendTo.irc(channel.ircChan, '<' + getName(msg.from) + '>: ' +
                    '(Photo) ' + path);
            });
        } else if (msg.sticker) {
            sendTo.irc(channel.ircChan, '<' + getName(msg.from) + '>: ' +
                '(Sticker)');
        } else if (msg.video) {
            sendTo.irc(channel.ircChan, '<' + getName(msg.from) + '>: ' +
                '(Video, ' + msg.video.duration + 's)');
        } else if (msg.voice) {
            sendTo.irc(channel.ircChan, '<' + getName(msg.from) + '>: ' +
                '(Voice, ' + msg.audio.duration + 's)');
        } else if (msg.contact) {
            sendTo.irc(channel.ircChan, '<' + getName(msg.from) + '>: ' +
                '(Contact, ' + '"' + msg.contact.first_name + ' ' +
                msg.contact.last_name + '", ' +
                msg.contact.phone_number + ')');
        } else if (msg.location) {
            sendTo.irc(channel.ircChan, '<' + getName(msg.from) + '>: ' +
                '(Location, ' + 'lon: ' + msg.location.longitude +
                              ', lat: ' + msg.location.latitude + ')');
        } else if (msg.new_chat_participant) {
            sendTo.irc(channel.ircChan, getName(msg.new_chat_participant) +
                ' was added by: ' + getName(msg.from));
        } else if (msg.left_chat_participant) {
            sendTo.irc(channel.ircChan, getName(msg.left_chat_participant) +
                ' was removed by: ' + getName(msg.from));
        } else {
            sendTo.irc(channel.ircChan, '<' + getName(msg.from) + '>: ' + msg.text);
        }
{'fraction_non_alphanumeric': 0.15501848678870953, 'fraction_numerical': 0.001262512399675354, 'mean_word_length': 1.6429933269780743, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7157165', 'n_tokens_mistral': 3501, 'n_tokens_neox': 3327, 'n_words': 669}