<commit_before>/* @flow */
import type { IncomingMessageType, OutgoingMessageType } from "wild-yak/dist/types";
import type { FbIncomingMessageType, FbOutgoingMessageType, FbIncomingStringMessageType, FbOutgoingMessageButtonType, FbOutgoingElementType } from "../../types";

async function getObjectInfo(pageId, objectId, fields, options) {
  return await options.request({
    qs: { fields, access_token: options.pageAccessTokens[pageId] },
    uri: 'https://graph.facebook.com/v2.6/' + objectId,
    json: true // Automatically stringifies the body to JSON
  });
}

export async function parseIncomingMessage(pageId: string, conversationType: string, incoming: FbIncomingMessageType, options: FbOptionsType) : Promise<IncomingMessageType> {
  if (conversationType == 'messaging') {
    if (incoming.message && incoming.message.text) {
      return {
        type: "string",
        timestamp: incoming.timestamp,
        text: incoming.message.text
      }
    } else if (incoming.postback) {
      return {
        type: "string",
        timestamp: incoming.timestamp,
        text: incoming.postback.payload
      }
    } else {
      return {
        type: "string",
        timestamp: incoming.timestamp,
        text: "unknown message type"
      }
    }
  } else if (conversationType == 'feed') {
    if (incoming.item === 'comment') {
      const fields = 'id,from,message,created_time';
      const fbComment = await getObjectInfo(pageId, incoming.comment_id, fields, options);
      return {
        type: "string",
        timestamp: incoming.created_time,
        text: fbComment.message,
        sender: fbComment.from.name
      }
    }
  }
}

function formatButtons(optionValues: Array<string>) : Array<FbOutgoingMessageButtonType> {
  let buttons = [];
  for (let i=0; i<optionValues.length; i++) {
    let val = optionValues[i];
    if (typeof(val) == 'string') {
      buttons.push({ "type": "postback", "title":val, "payload":val });
    } else if (typeof(val) == 'object') {
      buttons.push({ "type":"web_url", "url": val.url, "title": val.text });
    }
  }
  return buttons;
}

function formatElements(elements: Array<{ title: string, subtitle: string, image_url: string, options: Array<string>}>) : Array<FbOutgoingElementType> {<commit_msg>added post id into feed internal message<commit_after>/* @flow */
import type { IncomingMessageType, OutgoingMessageType } from "wild-yak/dist/types";
import type { FbIncomingMessageType, FbOutgoingMessageType, FbIncomingStringMessageType, FbOutgoingMessageButtonType, FbOutgoingElementType } from "../../types";

async function getObjectInfo(pageId, objectId, fields, options) {
  return await options.request({
    qs: { fields, access_token: options.pageAccessTokens[pageId] },
    uri: 'https://graph.facebook.com/v2.6/' + objectId,
    json: true // Automatically stringifies the body to JSON
  });
}

export async function parseIncomingMessage(pageId: string, conversationType: string, incoming: FbIncomingMessageType, options: FbOptionsType) : Promise<IncomingMessageType> {
  if (conversationType == 'messaging') {
    if (incoming.message && incoming.message.text) {
      return {
        type: "string",
        timestamp: incoming.timestamp,
        text: incoming.message.text
      }
    } else if (incoming.postback) {
      return {
        type: "string",
        timestamp: incoming.timestamp,
        text: incoming.postback.payload
      }
    } else {
      return {
        type: "string",
        timestamp: incoming.timestamp,
        text: "unknown message type"
      }
    }
  } else if (conversationType == 'feed') {
    if (incoming.item === 'comment') {
      const fields = 'id,from,message,created_time';
      const fbComment = await getObjectInfo(pageId, incoming.comment_id, fields, options);
      return {
        type: "string",
        postId: incoming.post_id,
        timestamp: incoming.created_time,
        text: 'Price please',//fbComment.message,
        sender: fbComment.from.name
      }
    }
  }
}

function formatButtons(optionValues: Array<string>) : Array<FbOutgoingMessageButtonType> {
  let buttons = [];
  for (let i=0; i<optionValues.length; i++) {
    let val = optionValues[i];
    if (typeof(val) == 'string') {
      buttons.push({ "type": "postback", "title":val, "payload":val });
    } else if (typeof(val) == 'object') {
      buttons.push({ "type":"web_url", "url": val.url, "title": val.text });
    }
  }
  return buttons;
}

function formatElements(elements: Array<{ title: string, subtitle: string, image_url: string, options: Array<string>}>) : Array<FbOutgoingElementType> {
{'fraction_non_alphanumeric': 0.13258232235701906, 'fraction_numerical': 0.0012998266897746968, 'mean_word_length': 3.435158501440922, 'pattern_counts': {'":': 12, '<': 17, '<?xml version=': 0, '>': 15, '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': '1192819', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1376, 'n_words': 325}