【问题标题】:TypeError: mitsukuApi.send is not a function [closed]TypeError:mitsukuApi.send 不是函数[关闭]
【发布时间】:2019-01-22 00:38:28
【问题描述】:

我尝试使用mitsuku chatbot's API 并测试以下内容。

var m = require("mitsuku-api")

m.send('hello world')
  .then(function(response){
    console.log(response);
  });

我正在使用 ubuntu 控制台并安装了 nodejs 和 npm。但是当我尝试运行上述内容时遇到以下错误。

/home/manuelanayantarajeyaraj/node_modules/mitsuku-api/mitsukutest.js:3 mitsukuApi.send('你好世界') ^

TypeError: mitsukuApi.send 不是函数 在对象。 (/home/manuelanayantarajeyaraj/node_modules/mitsuku-api/mitsukutest.js:3:12)
在 Module._compile (module.js:410:26)
在 Object.Module._extensions..js (module.js:417:10)
在 Module.load (module.js:344:32)
在 Function.Module._load (module.js:301:12)
在 Function.Module.runMain (module.js:442:10)
启动时 (node.js:136:18)
在 node.js:966:3

mitsuku.js 文件

'use strict';

var Promise = require('bluebird'),
    cheerio = require('cheerio'),
    superagent = require('superagent');

var ENDPOINT_CHAT_MITSUKU = 'https://kakko.pandorabots.com/pandora/talk?botid=87437a824e345a0d&skin=chat',
    MESSAGE_REGEX = /(Mitsuku -(.*))/,
    MESSAGE_REJECT_REGEX = /(x(.*)x[^\s]+)|(\|)|(BYESPLIT X1234)/ig,
    MESSAGE_SENDER_TAG = 'You -';

function getRawHtmlForMessage(mitsuku, message) {
    return new Promise(function (resolve, reject) {
        if (!mitsuku) {
            return reject(new Error('Mitsuku cannot be null'));
        }
        if (!message) {
            return reject(new Error('Message cannot be null or empty'));
        }

        var agent = mitsuku._agent,
            endpoint = mitsuku._endpoint,
            req;

        req = agent.post(endpoint);
        agent.attachCookies(req);
        req.set('Content-Type', 'application/x-www-form-urlencoded')
            .send({ message: message })
            .end(function (err, res) {
                if (err) {
                    return reject(err);
                }
                agent.saveCookies(res);
                resolve(res.text);
            });
    });
}

function parseMessageFromHtml(html) {
    var conv = cheerio.load(html)('body')
        .find('p')
        .text()
        .trim();

    var match = MESSAGE_REGEX.exec(conv),
        message,
        prevMessageStart;

    if (match && match.length > 0) {
        message = match[match.length - 1];
        prevMessageStart = message.indexOf(MESSAGE_SENDER_TAG);
        if (prevMessageStart != -1) {
            message = message.substr(0, prevMessageStart);
        }
        return message.replace(MESSAGE_REJECT_REGEX, '').trim();
    } else {
        throw new Error("Could not parse Mitsuku response");
    }
}

/**
 * Create new Mitsuku API for the given options.
 *
 * @param options
 * @constructor
 */
function Mitsuku(options) {
    options = options || {};
    this._tag = options.tag || 'Anonymous';
    this._agent = superagent.agent();
    this._endpoint = options.endpoint || ENDPOINT_CHAT_MITSUKU;
}

/**
 * Send a message to this {@link Mitsuku} instance.
 *
 * @param message
 * @return bluebird message response promise
 */
Mitsuku.prototype.send = function(message) {
    return getRawHtmlForMessage(this, message)
        .then(parseMessageFromHtml)
};

/**
 * Get the tag this {@link Mitsuku} was setup with.
 *
 * @returns {*|string}
 */
Mitsuku.prototype.getTag = function() {
    return '' + this._tag;
};

/**
 * Describe this {@link Mitsuku} instance.
 *
 * @returns {*|string}
 */
Mitsuku.prototype.toString = function() {
    return this.getTag();
};

/**
 * Mitsuku API module
 * @module lib/mitsuku
 */

/**
 * Create new instance of {@link Mitsuku} for the given options.
 *
 * @param options
 * @returns {Mitsuku}
 */
module.exports = function newInstance(options) {
    return new Mitsuku(options);
};

在这方面的任何帮助将不胜感激。

【问题讨论】:

    标签: javascript node.js function api typeerror


    【解决方案1】:

    我是 Mitsuku 的开发人员,我阻止了您的代码工作。不先问别人就偷别人的工作是不好的。

    我原以为我们放在 Github 存储库上的“停止和终止”通知会充分解释这一点:https://github.com/eleventigers/mitsuku-api/issues/11

    如果您想在工作中使用 Mitsuku,请通过 info@pandorabots.com 与我联系,我们可以讨论定价。

    【讨论】:

    • 嗨,史蒂夫,我并不是说 Mitsuku 出了什么问题,只是我不知道如何让它运行。此外,我是 Mitsuku 的忠实粉丝,并没有专业地使用它,所以我希望它没问题。对不起,如果这是错误的意思。
    【解决方案2】:

    根据你的mitsuku.js,因为你导出为函数,所以需要在require语句中加上括号()

    const m = require('./mitsuku')(); // parentheses added
    

    【讨论】:

    • 非常感谢您的回复。但是当我如图所示更改 js 时出现以下错误。 Unhandled rejection Error: Could not parse Mitsuku response at parseMessageFromHtml (/home/manuelanayantarajeyaraj/node_modules/mitsuku-api/lib/mitsuku.js:57:15) ...
    猜你喜欢
    • 2021-04-28
    • 2019-10-26
    • 2021-11-04
    • 2020-04-02
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    相关资源
    最近更新 更多