juzibot / xiaoju

17 stars 2 forks source link

problems sending a message #13

Closed dcsan closed 5 years ago

dcsan commented 5 years ago

I want to test sending a simple message. first, I can't see anywhere in the main dashboard to get chatId from the chats. but I assume I can use one from an incoming message?

318|recobo | Thu, 13 Jun 2019 12:28:05 GMT orange POST in/message { data:
318|recobo |    { messageId: '1067100472',
318|recobo |      chatId: 'XXX23934bd6faa1c4e8f7XXX',
318|recobo |      roomTopic: '',
318|recobo |      roomId: '',
318|recobo |      contactName: 'DC | CTO | Rikai Labs',
318|recobo |      contactId: 'wxid_xet7g1sXXXX',
318|recobo |      payload: { text: 'from DC' },
318|recobo |      type: 7,
318|recobo |      timestamp: 1560428884000,
318|recobo |      token: 'XXX220f094e5100xxxxXXXX' } }
318|recobo | Thu, 13 Jun 2019 12:28:05 GMT morgan log request
318|recobo | POST /api/bot/in/message 200 23.777 ms - 2

so I use that chatId to try and send a message, with the token (actually same token in dashboard is in the incoming message... maybe a security risk?)


send: { chatId: 'XXX23934bd6faa1c4e8f7XXX',
  token: 'XXX220f094e5100xxxXXXX',
  messageType: 0,
  payload: { text: 'Whats up!' } }

uri https://api.botorange.com/xiaoju/message/send

But I just get a timeout...

  follow-redirects options { protocol: 'https:',
  maxRedirects: 21,
  maxBodyLength: 10485760,
  path: '/xiaoju/message/send',
  method: 'POST',
  headers:
   { Accept: 'application/json, text/plain, */*',
     'Content-Type': 'application/json;charset=utf-8',
     'User-Agent': 'axios/0.19.0',
     'Content-Length': 119 },
  agent: undefined,
  auth: undefined,
  hostname: 'api.botorange.com',
  port: null,
  nativeProtocols:
   { 'http:':
      { _connectionListener: [Function: connectionListener],
        METHODS: [Array],
        STATUS_CODES: [Object],
        Agent: [Function],
        ClientRequest: [Function],
        globalAgent: [Agent],
        IncomingMessage: [Function],
        OutgoingMessage: [Function],
        Server: [Function],
        ServerResponse: [Function],
        createServer: [Function: createServer],
        get: [Function: get],
        request: [Function: request] },
     'https:':
      { Agent: [Function],
        globalAgent: [Agent],
        Server: [Function],
        createServer: [Function: createServer],
        get: [Function: get],
        request: [Function: request] } } } +0ms
  morgan log request +52s
GET /api/bot/out - - ms - -
(node:60090) UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT 52.80.36.166:443
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14)
(node:60090) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:60090) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
dcsan commented 5 years ago

hi! I created a small repo to act as a test. you can see the code to send a message here. however sending a message just times out.

can you confirm that function is working?

I can receive messages but not send them.

https://github.com/dcsan/ackbot/blob/master/server/routes/bot/out.js#L27

router.get('/bot/out', function(req, res, next) {
  let sendUrl = config.sendUrl
  let data = {
    "chatId": config.testChatId,
    "token": config.token,
    "messageType": 0, // MessageType, check below
    "payload": {
        "text": "from code"
    }
  }

  debug.log('send uri:', sendUrl)
  debug.log('send data:', data)

  axios({
    method: 'post',
    url: sendUrl,
    timeout: 3000,
    data: data
  })
  .then( function(response) {
    debug.log('send response', response)
    res.json(data)
  })
  .catch(function (err) {
    debug.error('failed to send')
    // TODO - check what type of error
    res.status(500).json({
      status: 500,
      msg: 'failed to send / timeout'
    })
  })
dcsan commented 5 years ago

docs

发送消息

发消息url为 https://api.botorange.com/xiaoju/message/send

请求数据为json格式,如下

{
    "chatId": "bcdw2j234ko1",
    "token": "abcd",
    "messageType": 1, // MessageType, check below
    "payload": {
        "text": "Hello World"
    }
}

返回格式如下

{
    "code": 0,
    "message": ""
}

chatId为系统中生成的id,需提前获取或一直保存 请求中的messageTypepayload遵循如下格式,messageType需要和payload格式对应,否则会报错

enum MessageType {
    TEXT = 0,
    IMAGE = 1,
    URL_LINK = 2,
    FILE = 3,
}

TextPayload {
    text: string,
    mention?: string[],
}

ImagePayload {
    url: string,
}

UrlPayload {
    sourceUrl: string,
    title: string,
    summary: string,
    imageUrl: string,
}

FilePayload {
    url: string,
    name: string,
}
windmemory commented 5 years ago

The doc was wrong, I updated the doc and related server config, now it should be working. Please refer to the doc for the latest endpoint of sending messages.

It is ex-api.botorange.com/message/send now.

dcsan commented 5 years ago

works! docs: https://github.com/botorange/xiaoju/wiki/API-Doc