<commit_before>const Telegraf = require('telegraf');
const { memorySession, Markup, Extra } = require('telegraf');

const express = require('express');
const expressApp = express();

const __ = require('multi-lang')('lang/lang.json', 'en', false);
const opizo = require('opizo-api');
const imdb = require('imdb-search');

let userManager = require('./user-manager-db');
let imageUploader = require('./image-uploader');
let templateManager = require('./templates/manager');

// Enter bot API Token on here or add as environment varialbe
const BOT_API_TOKEN = process.env.BOT_API_TOKEN || '';
const PORT = process.env.PORT || 3000;
const URL = process.env.URL || 'https://free-night.herokuapp.com';

const bot = new Telegraf(BOT_API_TOKEN);

bot.telegram.setWebhook(`${URL}/bot${BOT_API_TOKEN}`);

expressApp.use(bot.webhookCallback(`/bot${BOT_API_TOKEN}`));

bot.use(memorySession());

// Use array to keep track of user on registration
let registerSession = [];

// Get user lang from user manager, use this to simple access
function getLang(userId) {
  return userManager.getLang(userId);
}

// Put user on registration mode and ask for confirmation
bot.command('register', (ctx) => {
  if (typeof (ctx.message.chat.type) !== 'undefined' &&
    ctx.message.chat.type === 'private') {
    let userId = ctx.message.from.id;

    // Make sure user is in the list
    userManager.addUser(userId);

    registerSession[userId] = null;

    ctx.reply(__('register-start', { userId }, getLang(userId)));
  }
});

// If on registration mode and chatKey was not exists before confirm it
bot.hears(/^\/confirm (.+)$/, (ctx) => {
  let userId = ctx.message.from.id;
  let chatKey = ctx.match[1];

  if (userId in registerSession && registerSession[userId]) {
    let user = {
      _id: userId,
      keys: [{
        key: chatKey,
        kid: registerSession[userId]
      }]
    };

    if (userManager.addKey(user)) {
      ctx.telegram.sendMessage(userId,
        __('register-confirm-success', { chatKey }, getLang(userId)));

      delete registerSession[userId];
    } else {
      ctx.telegram.sendMessage(userId,
        __('register-confirm-failed', getLang(userId)));
    }
  } else {
    ctx.reply(__('register-not-started', getLang(userId)));
  }
});

// Cancel Registration
bot.command('cancelReg', (ctx) => {
  let userId = ctx.message.from.id;
  if (userId in registerSession) {
    delete registerSession[userId];

    bot.telegram.sendMessage(userId,
      __('register-cancel', getLang(userId)));
  }
});

bot.command('start', (ctx) => {
  let userId = ctx.message.from.id;

  if (ctx.message.chat.type === 'private') {
    ctx.session.state = null;
    ctx.session.store = null;
  }

  userManager.addUser(userId);

  ctx.reply(__('welcome'), getLang(userId));
});

bot.command('help', (ctx) => {
  ctx.reply(__('help', getLang(ctx.message.from.id)), { parse_mode: 'Markdown', disable_web_page_preview: true });
});

bot.hears(/^\/setLang (.+)$/, (ctx) => {
  let userId = ctx.message.from.id;
  let lang = ctx.match[1];

  if (lang === 'fa' || lang === 'en') {
    userManager.setLang(userId, lang);
    ctx.reply(__('setLang-success', lang))
  } else {
    ctx.reply(__('setLang-failed', getLang(userId)));
  }
});

bot.hears(/^\/setSignature (.+)$/, (ctx) => {
  let userId = ctx.message.from.id;
  let signature = ctx.match[1];

  userManager.setSignature(userId, signature);
  ctx.reply(__('setSignature-success', getLang(userId)));
});

function isLinkValid(url) {
  // If url start with http:// or https://
  return url.startsWith('http://') || url.startsWith('https://') ? url : null;
}

function shortener(ctx, userId, url, onLink = false) {
  if (isLinkValid(url)) {
    ctx.reply(__('shortLink-received', getLang(userId)));

    opizo.extra(url)
      .then((result) => {
        let answerMsg = __('shortLink-success', { 'shortUrl': result.shortUrl, 'url': result.url }, getLang(userId));

        if (result.fileInfo) {
          answerMsg = `File info: ${result.fileInfo.name} - ${result.fileInfo.sizeInMB} \n\n` + answerMsg;

          if (onLink) {
            ctx.session.tmpLink.label = result.fileInfo.name;
            ctx.session.tmpLink.size = result.fileInfo.sizeInMB;
          }
        }

        ctx.reply(answerMsg)
          .then(res => {
            if (onLink) {
              ctx.session.tmpLink.setNext(result.shortUrl);
              linkNextPrompt(ctx, userId);
            }
          });
      })
      .catch((err) => {
        // Code 1 for server error and code 2 for link access error
        if (err.code === 1) {
          ctx.reply(__('shortLink-error-1', { "errMessage": err.message, "url": err.result.url }, getLang(userId)));
        } else if (err.code === 2) {
          ctx.reply(__('shortLink-error-2', { "errMessage": err.message, "shortUrl": err.result.shortUrl, "url": err.result.url }, getLang(userId)))
            .then(res => {
              if (onLink) {
                ctx.session.tmpLink.setNext(err.result.shortUrl);
                linkNextPrompt(ctx, userId);
              }
            });
        }
      });

  } else {
    ctx.reply(__('invalidLink', getLang(userId)));
  }
}

bot.hears(/^\/shortLink (.+)$/, (ctx) => {
  let userId = ctx.message.from.id;

  shortener(ctx, userId, ctx.match[1], false);
});

bot.on('photo', (ctx) => {<commit_msg>Fox long wait on get file info to request just shortlink without file info<commit_after>const Telegraf = require('telegraf');
const { memorySession, Markup, Extra } = require('telegraf');

const express = require('express');
const expressApp = express();

const __ = require('multi-lang')('lang/lang.json', 'en', false);
const opizo = require('opizo-api');
const imdb = require('imdb-search');

let userManager = require('./user-manager-db');
let imageUploader = require('./image-uploader');
let templateManager = require('./templates/manager');

// Enter bot API Token on here or add as environment varialbe
const BOT_API_TOKEN = process.env.BOT_API_TOKEN || '';
const PORT = process.env.PORT || 3000;
const URL = process.env.URL || 'https://free-night.herokuapp.com';

const bot = new Telegraf(BOT_API_TOKEN);

bot.telegram.setWebhook(`${URL}/bot${BOT_API_TOKEN}`);

expressApp.use(bot.webhookCallback(`/bot${BOT_API_TOKEN}`));

bot.use(memorySession());

// Use array to keep track of user on registration
let registerSession = [];

// Get user lang from user manager, use this to simple access
function getLang(userId) {
  return userManager.getLang(userId);
}

// Put user on registration mode and ask for confirmation
bot.command('register', (ctx) => {
  if (typeof (ctx.message.chat.type) !== 'undefined' &&
    ctx.message.chat.type === 'private') {
    let userId = ctx.message.from.id;

    // Make sure user is in the list
    userManager.addUser(userId);

    registerSession[userId] = null;

    ctx.reply(__('register-start', { userId }, getLang(userId)));
  }
});

// If on registration mode and chatKey was not exists before confirm it
bot.hears(/^\/confirm (.+)$/, (ctx) => {
  let userId = ctx.message.from.id;
  let chatKey = ctx.match[1];

  if (userId in registerSession && registerSession[userId]) {
    let user = {
      _id: userId,
      keys: [{
        key: chatKey,
        kid: registerSession[userId]
      }]
    };

    if (userManager.addKey(user)) {
      ctx.telegram.sendMessage(userId,
        __('register-confirm-success', { chatKey }, getLang(userId)));

      delete registerSession[userId];
    } else {
      ctx.telegram.sendMessage(userId,
        __('register-confirm-failed', getLang(userId)));
    }
  } else {
    ctx.reply(__('register-not-started', getLang(userId)));
  }
});

// Cancel Registration
bot.command('cancelReg', (ctx) => {
  let userId = ctx.message.from.id;
  if (userId in registerSession) {
    delete registerSession[userId];

    bot.telegram.sendMessage(userId,
      __('register-cancel', getLang(userId)));
  }
});

bot.command('start', (ctx) => {
  let userId = ctx.message.from.id;

  if (ctx.message.chat.type === 'private') {
    ctx.session.state = null;
    ctx.session.store = null;
  }

  userManager.addUser(userId);

  ctx.reply(__('welcome'), getLang(userId));
});

bot.command('help', (ctx) => {
  ctx.reply(__('help', getLang(ctx.message.from.id)), { parse_mode: 'Markdown', disable_web_page_preview: true });
});

bot.hears(/^\/setLang (.+)$/, (ctx) => {
  let userId = ctx.message.from.id;
  let lang = ctx.match[1];

  if (lang === 'fa' || lang === 'en') {
    userManager.setLang(userId, lang);
    ctx.reply(__('setLang-success', lang))
  } else {
    ctx.reply(__('setLang-failed', getLang(userId)));
  }
});

bot.hears(/^\/setSignature (.+)$/, (ctx) => {
  let userId = ctx.message.from.id;
  let signature = ctx.match[1];

  userManager.setSignature(userId, signature);
  ctx.reply(__('setSignature-success', getLang(userId)));
});

function isLinkValid(url) {
  // If url start with http:// or https://
  return url.startsWith('http://') || url.startsWith('https://') ? url : null;
}

function shortener(ctx, userId, url, onLink = false) {
  if (isLinkValid(url)) {
    ctx.reply(__('shortLink-received', getLang(userId)));

    opizo(url)
      .then((result) => {
        ctx.reply(__('shortLink-success', { 'shortUrl': result.shortUrl, 'url': result.url }, getLang(userId)))
          .then(res => {
            if (onLink) {
              ctx.session.tmpLink.setNext(result.shortUrl);
              linkNextPrompt(ctx, userId);
            }
          });
      })
      .catch((err) => {
        ctx.reply(__('shortLink-error-1', { "errMessage": err.message, "url": err.result.url }, getLang(userId)));
      });

  } else {
    ctx.reply(__('invalidLink', getLang(userId)));
  }
}

bot.hears(/^\/shortLink (.+)$/, (ctx) => {
  let userId = ctx.message.from.id;

  shortener(ctx, userId, ctx.match[1], false);
});

bot.on('photo', (ctx) => {
{'fraction_non_alphanumeric': 0.17811550151975683, 'fraction_numerical': 0.0023302938196555216, 'mean_word_length': 3.2492466637968143, 'pattern_counts': {'":': 7, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 6, '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': '4053031', 'n_tokens_mistral': 3216, 'n_tokens_neox': 3251, 'n_words': 664}