Не является ошибкой функции с module.exports - PullRequest
0 голосов
/ 13 июня 2018

, когда я пытаюсь 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 голосов
/ 13 июня 2018

Попробуйте, вы пытаетесь сделать что-то, что не поддерживается.

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
}
...