javascript - Rocket.Chat 集成中的 API 调用

标签 javascript meteor rocket.chat asynchronous-javascript

我正在尝试进一步扩展 telegram-rocket.chat 桥并且需要为此调用一些 API。为此,rocket.chat 公开了一个名为 HTTP 的 Meteor.js 包装器。

此代码片段是一个传出 Hook ,用于处理用户发送的消息,让我转换消息并传递更改后的文本。

prepare_outgoing_request({request}) 被 rocket.chat Hook 调用,我想在其中调用一个 API,将表情符号代码解析为实际的表情符号字符:“:see_no_evil: to 🙈”

/** Global Helpers
 *
 * console - A normal console instance
 * _       - An underscore instance
 * s       - An underscore string instance
 * HTTP    - The Meteor HTTP object to do sync http calls
 *           https://docs.meteor.com/api/http.html
 */


class Script {
    request_emojitext(emoji_code) {
       console.log(`called: request_emojitext('${emoji_code}')`);
       const url = `https://www.emojidex.com/api/v1/emoji/${emoji_code}`;
  
       const response = HTTP.call('GET', url);

       console.log(`Emoji Response: ${response.constructor.name} => ${JSON.stringify(response)}`);
      // Emoji Response: Object => {"error":{}}             
       return response;
    }
  
    /**
    	request.params            {object}
     	request.method            {string}
    	request.url               {string}
    	request.headers           {object}
    	*/
    prepare_outgoing_request({ request }) {
      	const emojiResponse = this.request_emojitext('see_no_evil');
      	const emojiCharacter = emojiResponse.content.emoji;
        
        return {
          // https://core.telegram.org/bots/api
          url: `${request.url}&text=${emojiCharacter}`,
          method: 'GET'
        };
    }
}

Meteor documentation指出:

// Asynchronous call
Meteor.call('foo', 1, 2, (error, result) => { ... });

// Synchronous call
const result = Meteor.call('foo', 1, 2);

/*
On the client, if you do not pass a callback and you are not 
inside a stub, call will return undefined, and you will have 
no way to get the return value of the method. That is because
the client doesn’t have fibers, so there is not actually any 
way it can block on the remote execution of a method.
*/

我不确定如何在这里进行,因为我对异步编程还不是很满意。在结果实际可用之前,我将如何阻止,或者是否有我完全想念的不同方法?

最佳答案

如文档所述,无法在客户端进行阻止——浏览器根本不为此实现任何机制。所以问题是,是什么让你很难处理客户端的延迟,直到回调被调用。当然,典型的模式是在调用时将客户端切换到某种“等待”状态(例如,显示微调器),然后在回调触发时用结果更新页面(并隐藏微调器) ).

关于javascript - Rocket.Chat 集成中的 API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53140632/

相关文章:

JavaScript :Still showing previous results

JavaScript 和 jQuery : accessing class member variables from event callback

JavaScript 数组在循环外是正确的,但循环内未定义

javascript - 如何在 Meteor 中结束 session

c# - 如何将文件从 .NET 应用程序上传到 Rocket.Chat-Channel?

docker-compose - 管理员用户在火箭聊天中失败

Android 应用程序无法连接到 Rocket Chat 服务器

javascript - 限制 Fusion Tables 中的查询长度?

meteor - 有没有办法知道 meteor 订阅何时是 'valid' ?

javascript - 在路由到 Meteor 主页之前创建登录/注册页面?