Wechaty: how to send mesage without onMessage ?

Created on 28 Apr 2017  路  5Comments  路  Source: wechaty/wechaty

how to send message directly to a room ? like with webhook call or some things shell command? what's the best way?

question

Most helpful comment

see more related issues on FAQ: https://github.com/Chatie/wechaty/wiki/FAQ#5-how-to-send-mesage-without-onmessage

All 5 comments

It's easy.

const r = await Room.find('room name')
r.say('hello room')

See room-bot.ts - Manage Chat Room at https://github.com/Chatie/wechaty/wiki/Example

so, the only way to send message to room is start timer looper inside onlogin ? is possible to make wechaty handle webhook, for example after ci build successful with webhook call wechaty cansend message to room depend request params?

You can send a message only after login, but there's others way to do this instead of start timer inside onlogin.

like this:

let isLogined = false
let globalRoom = null

Wechaty.instance()
.on('login', async () => {
  isLogined = true
  initRoom()
})
.init()

function async initRoom() {
  globalRoom = await Room.find('room name')
  if (!globalRoom) {
    console.warn('room not found. retry after 3 seconds')
    setTimeout(initRoom, 3000)
  }
}

function sendToRoom(msg) {
  if (!isLogined) {
    console.error('need login first')
    return
  }

  if (!globalRoom) {
    console.log('room not found(yet), please send msg after Wechaty fully loaded.')
    return
  }

  globalRoom.say(msg)
}

app.get('/room', (req, res, done) => {
  globalRoom.say('received a get request!')
  done()
})

Just write from scratch, hope it could help you.

see more related issues on FAQ: https://github.com/Chatie/wechaty/wiki/FAQ#5-how-to-send-mesage-without-onmessage

cool! it's works. Thanks!

Was this page helpful?
0 / 5 - 0 ratings