<commit_before>const rp = require('request-promise')

module.exports = function () {
  const { commands, hears, on: reservedCallback } = this.actions

  if (!this.longPollParams) {
    return this.api('messages.getLongPollServer', {
      need_pts: 1,
      lp_version: 2,
      access_token: <PASSWORD>.token,
      v: 5.65
    })
      .then((body) => {
        if (!body.response || !body.response.server) {
          throw new Error(JSON.stringify(body))
        }

        this.longPollParams = body.response
        this.listen()
      })
      .catch((err) => {
        this.longPollParams = null
        return this.listen()
      })
  }

  return rp({
    url: `https://${this.longPollParams.server}`,
    qs: {
      ...this.longPollParams,
      act: 'a_check',
      wait: 25,
      mode: 2,
      version: 2
    },
    json: true
  })
    .then((body) => {
      if (body.failed) {
        if (body.failed && body.ts) {
          this.longPollParams.ts = body.ts
        } else {
          this.longPollParams = null
        }

        return this.listen()
      }

      if (body.ts) {
        this.longPollParams.ts = body.ts
      }

      this.listen()

      if (body.updates && body.updates.length) {
        body.updates.forEach((update) => {
          if (update[0] === 4 && (update[2] & 2) === 0) {
            const ctx = {
              user_id: update[3],
              body: update[5],
              attachments: update[6],
              date: update[4],
              message_id: update[1],
            }

            if (update[6].from) {
              ctx.from = +update[6].from
            }

            const getForward = (ctx) => {
              return new Promise((resolve) => {
                if (!ctx.attachments || !ctx.attachments.fwd) {
                  resolve(null)
                }

                this.execute('messages.getById', {
                  message_ids: ctx.message_id,
                  v: 5.67
                }, this.token, (data) => {
                  resolve(this.getLastMessage(data.items[0]))
                })
              })
            }

            getForward(ctx)
              .then((forward) => {
                if (forward) {
                  ctx.forward = forward
                  ctx.body = forward.body
                }

                const attachmentsKeys = Object.keys(ctx.attachments).filter(key => key.search('attach') > -1 && key.search('type') === -1 && key.search('kind') === -1)
                const attachments = []

                attachmentsKeys.forEach((key) => {
                  const file = {}

                  file['type'] = ctx.attachments[`${key}_type`]
                  file[file.type] = ctx.attachments[key]

                  attachments.push(file)
                })

                ctx.attachments = attachments
                ctx.body = ctx.body.replace(/<br>/g, '\n')

                ctx.reply = (message, attachment) => this.reply(ctx.user_id, message, attachment)
                ctx.sendMessage = (userId, message, attachment) => this.reply(userId, message, attachment)

                const message = ctx.body.toLowerCase()
                const commandKey = Object.keys(commands).filter(item => item.indexOf(message) > -1)[0]
                const commandCallback = commands[commandKey]
                const hearsKey = Object.keys(hears).filter((item) => {
                  const index = item.split(';')
                    .filter((item) => {
                      if (/^\/[\S]{1,}\/[a-z]{1,2}$/.test(item)) {<commit_msg>Add original message saver for forward case (#9)<commit_after>const rp = require('request-promise')

module.exports = function () {
  const { commands, hears, on: reservedCallback } = this.actions

  if (!this.longPollParams) {
    return this.api('messages.getLongPollServer', {
      need_pts: 1,
      lp_version: 2,
      access_token: this.token,
      v: 5.65
    })
      .then((body) => {
        if (!body.response || !body.response.server) {
          throw new Error(JSON.stringify(body))
        }

        this.longPollParams = body.response
        this.listen()
      })
      .catch((err) => {
        this.longPollParams = null
        return this.listen()
      })
  }

  return rp({
    url: `https://${this.longPollParams.server}`,
    qs: {
      ...this.longPollParams,
      act: 'a_check',
      wait: 25,
      mode: 2,
      version: 2
    },
    json: true
  })
    .then((body) => {
      if (body.failed) {
        if (body.failed && body.ts) {
          this.longPollParams.ts = body.ts
        } else {
          this.longPollParams = null
        }

        return this.listen()
      }

      if (body.ts) {
        this.longPollParams.ts = body.ts
      }

      this.listen()

      if (body.updates && body.updates.length) {
        body.updates.forEach((update) => {
          if (update[0] === 4 && (update[2] & 2) === 0) {
            const ctx = {
              user_id: update[3],
              body: update[5],
              attachments: update[6],
              date: update[4],
              message_id: update[1],
            }

            if (update[6].from) {
              ctx.from = +update[6].from
            }

            const getForward = (ctx) => {
              return new Promise((resolve) => {
                if (!ctx.attachments || !ctx.attachments.fwd) {
                  resolve(null)
                }

                this.execute('messages.getById', {
                  message_ids: ctx.message_id,
                  v: 5.67
                }, this.token, (data) => {
                  resolve(this.getLastMessage(data.items[0]))
                })
              })
            }

            getForward(ctx)
              .then((forward) => {
                if (forward) {
                  ctx.forward = forward
                  ctx.original = { body: ctx.body }
                  ctx.body = forward.body
                }

                const attachmentsKeys = Object.keys(ctx.attachments).filter(key => key.search('attach') > -1 && key.search('type') === -1 && key.search('kind') === -1)
                const attachments = []

                attachmentsKeys.forEach((key) => {
                  const file = {}

                  file['type'] = ctx.attachments[`${key}_type`]
                  file[file.type] = ctx.attachments[key]

                  attachments.push(file)
                })

                ctx.attachments = attachments
                ctx.body = ctx.body.replace(/<br>/g, '\n')

                ctx.reply = (message, attachment) => this.reply(ctx.user_id, message, attachment)
                ctx.sendMessage = (userId, message, attachment) => this.reply(userId, message, attachment)

                const message = ctx.body.toLowerCase()
                const commandKey = Object.keys(commands).filter(item => item.indexOf(message) > -1)[0]
                const commandCallback = commands[commandKey]
                const hearsKey = Object.keys(hears).filter((item) => {
                  const index = item.split(';')
                    .filter((item) => {
                      if (/^\/[\S]{1,}\/[a-z]{1,2}$/.test(item)) {
{'fraction_non_alphanumeric': 0.14337466908178906, 'fraction_numerical': 0.009335376898425526, 'mean_word_length': 1.433220338983051, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 40, 'https://': 2, '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': '1739347', 'n_tokens_mistral': 2150, 'n_tokens_neox': 2012, 'n_words': 372}