How to execute file-reading processing every time when post messages on SLack with fs modules in Node.js

68 Views Asked by At

Recently, I've been creating Slack Bot.

And then, I have a question today, about how do I execute file-reading processing every time when post the messages and when content of file change during the program execution, to return the result.

'use strict';
import bolt from '@slack/bolt';
import dotenv from 'dotenv';
import chalk from 'chalk';
dotenv.config();

import fs from 'node:fs';
import readline from 'readline';
const rs = fs.createReadStream('./meaning-data.csv');
const rl = readline.createInterface({ input: rs});
const app = new bolt.App(
  { token: process.env.SLACK_BOT_TOKEN, 
    appToken: process.env.SLACK_APP_TOKEN, 
    socketMode: true, 
    logLevel: 'debug' 
  });     
   
  const meaningObject = {};//Object

    //display-meaning function
     app.message(/mea (.+)/i, async({message, say}) => {
      const userInput = await message.text.match(/mea (.+)/i)[1];
        fs.readFile('./meaning-data.csv', 'utf-8', async(err, lineString) => {
           // values = [index, meaning, synonyms, URL];
          const values = lineString.split(',');
          const index = values[0];
          const meaning = values[1];
          const synonyms = values[2];
          const  URL = values[3];
          // meaningObject[index] = {index: index, meaning: meaning, synonyms: synonyms, URL: URL};
          meaningObject[index] = {index, meaning, synonyms, URL};
        });  
          await say(`Certainly <@${message.user}>, Here's meaning of *${meaningObject[userInput].index}*.`);  
          await say(`*Meaning:* ${meaningObject[userInput].meaning}`);
          await say(`*Synonyms:* ${meaningObject[userInput].synonyms}`);
          await say(`*URL:* ${meaningObject[userInput].URL}`);
        });   

 app.start();
 app.message(/mea (.+)/i, async({message, say}) => {
      const userInput = await message.text.match(/mea (.+)/i)[1];
      readFileProcessing(userInput, (value1, value2, value3, value4) => {
        await say(`Certainly <@${message.user}>, Here's meaning of *${value1}*.`);  
        await say(`*Meaning:* ${value2}`);
        await say(`*Synonyms:* ${value3}`);
        await say(`*URL:* ${value4}`);
      });  
    }); 

function readFileProcessing(word, callback) {
      fs.readFile('./meaning-data.csv', 'utf-8', async(err, lineString) => {
        // values = [index, meaning, synonyms, URL];
        const values = lineString.split(',');
        const index = values[0];
        const meaning = values[1];
        const synonyms = values[2];
        const  URL = values[3];
        // meaningObject[index] = {index: index, meaning: meaning, synonyms: synonyms, URL: URL};
        meaningObject[index] = {index, meaning, synonyms, URL};
        const value1 = meaningObject[word].index;
        const value2 = meaningObject[word].meaning;
        const value3 = meaningObject[word].synonyms;
        const value4 = meaningObject[word].URL;
        callback(value1,value2,value3,value4);
      });  
    } 

I've tried to include fs.readFile() function in the `app.message(

Though, fs.readFile() function executed just at once for some reason, even if I included fs.readFile() function in the app.message().

And as you can see the second code above, I also tried to round up such as file-related processing as the one readFileProcessing()function, and then result of processing in function send to the callback function to use as the callback function in app.message() function.

0

There are 0 best solutions below