与module.exports不是函数错误

时间:2018-06-12 23:54:14

标签: node.js facebook-messenger dialogflow

当我在callSendAPI中尝试waether.js时,它显示的错误不是callSendAPI不是函数

weather.js

const request = require('request');
const config = require('./config');
const messages = require('./messages');

weatherAsQuickReply = (messageData) => {
  let replies = [{
     "content_type": "location"
  }];

  let sendQuickReply = {
    recipient: {
      id: messageData.recipient.id
    },
    message: {
      text: messageData.message.text,
      quick_replies: replies
    }
  };

  messages.callSendAPI(sendQuickReply);
}

getWeather = (result, messageData) => {
  let city = result.parameters["geo-city"];
  if (city != '') {
    request({
      url: 'https://api.apixu.com/v1/current.json',
      qs: {
        q: city,
        key: config.WEATHER_APP_ID,
      },
    }, function (error, response, body) {
  if (!error && response.statusCode === 200) {
    let weather = JSON.parse(body);

    if (weather.hasOwnProperty("current")) {
      let reply = `Location: ${weather.location.region}
                     Temperature: ${weather.current.temp_c},
                     ${weather.current.condition.text}`;
      messageData.message.text = reply;
      messages.callSendAPI(messageData);
    };
  } else {
    return res.status(400).json({
      status: {
        code: 400,
        errorType: 'I failed to look up the city name.'
      }
    });

  }
});
}  else {
     weatherAsQuickReply(messageData);
   } 
}

module.exports = {
  weatherAsQuickReply: weatherAsQuickReply,
  getWeather: getWeather
}

message.js

const request = require('request');
const config = require('./config');
const weather = require('./weather');

const apiaiApp = require('apiai')(config.API_AI_CLIENT_ACCESS_TOKEN);

 callSendAPI = (messageData) => {
    request({
       uri: 'https://graph.facebook.com/v2.6/me/messages',
       qs: {
          access_token: config.FB_PAGE_TOKEN
       },
       method: 'POST',
       json: messageData

}, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        var recipientId = body.recipient_id;
        var messageId = body.message_id;

        if (messageId) {
            console.log("Successfully sent message with id %s to recipient %s",
                messageId, recipientId);
        } else {
            console.log("Successfully called Send API for recipient %s",
                recipientId);
        }
    } else {
        console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
    }
});
}

sendMessage = (event) => {
   let sender = event.sender.id;
   let text = event.message.text;

   let apiai = apiaiApp.textRequest(text, {
     sessionId: 'minaaaaa213'
});

apiai.on('response', (response) => {
    console.log(response);
    let aiText = response.result.fulfillment.speech;
    let result = response.result;
    let messageData = {
        recipient: {id: sender},
        message: {text: aiText}
    };
    if (response.result.action === 'weather') {
        console.log('weather');
        weather.getWeather(result, messageData);

    } else {
        callSendAPI(messageData);
    }

});

apiai.on('error', (error) => {
    console.log('err: ' + error);
});

apiai.end();
}

module.exports = {
  callSendAPI: callSendAPI,
  sendMessage: sendMessage
}

1 个答案:

答案 0 :(得分:0)

试试这个, 你想做一些不受支持的事情。

const request = require('request');
const config = require('./config');
const {weatherAsQuickReply,getWeather} = require('./temp2');   
const callSendAPI = (messageData) => {
        ......
}

const sendMessage = (event) => {
    let sender = event.sender.id;
    let text = event.message.text;

    let apiai = apiaiApp.textRequest(text, {
        sessionId: 'minaaaaa213'
    });

    apiai.on('response', (response) => {
        ...    
    });
}

module.exports = {
    callSendAPI,
    sendMessage
}

第二档

const request = require('request');
const config = require('./config');
const {callSendAPI,sendMessage} = require('./temp');

const weatherAsQuickReply = (messageData) => {
    ...
    callSendAPI(sendQuickReply);
}

const getWeather = (result, messageData) => {
   // Direct call Function like this
    weatherAsQuickReply(messageData);

}

module.exports = {
    weatherAsQuickReply,
    getWeather
}