首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rocket.Chat集成中的API调用

Rocket.Chat集成中的API调用
EN

Stack Overflow用户
提问于 2018-11-04 12:06:01
回答 1查看 1.5K关注 0票数 1

我试图进一步扩展电报-rocket.chat桥,并为此调用一些apis。为此,rocket.chat公开了一个名为Meteor.js的Meteor.js包装器。

这个代码片段是一个传出钩子,它处理用户发送的消息,让我转换消息并传递修改后的文本。

Prepare_outgoing_request({ rocket.chat })由rocket.chat钩子调用,我想在其中调用一个API,它将表情符号代码解析为实际的表情符号字符:":see_no_evil: to“。

代码语言:javascript
复制
/** 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'
        };
    }
}

流星文献指出:

代码语言:javascript
复制
// 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.
*/

我不知道如何在这里进行,因为我还不完全适应异步编程。我如何阻止,直到结果是实际可用的,或者有一种不同的方式做这件事,我完全错过了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-05 19:21:42

通过查看HTTP变量(PR #5876)的实现,我发现了这个问题。此外,还打开了异步调用的特性请求(第4775期)。

代码语言:javascript
复制
const response = HTTP('GET', 'https://www.emojidex.com/api/v1/emoji/sweat_smile');

这将同步执行API调用并返回结果对象:

代码语言:javascript
复制
{
  "result": {
    "statusCode": 200,
    "headers": {
      // ...
    },
    "data": {
      "code": "sweat smile",
      "moji": "",
      // ...
    }
  }
}

如果你想看完整的代码,你可以去看看吉特

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53140632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档