Listen to events
tip
The Wechaty Events are all the same acrossing Polyglot Wechaty programs!
Basic Events
scan
Event: QR code
TODO: introducing scan
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
import { ScanStatus } from 'wechaty'
async function onScan (
qrcode: undefined | string,
status: ScanStatus,
) {
console.info('Scan QR Code to login, status:', status, ScanStatus[status])
console.info('https://wechaty.js.org/qrcode/' + encodeURIComponent(qrcode))
}
bot.on('scan', onScan)
await bot.start()
const { ScanStatus } = require('wechaty')
async function onScan (
qrcode,
status,
) {
console.info('Scan QR Code to login, status:', status, ScanStatus[status])
console.info('https://wechaty.js.org/qrcode/' + encodeURIComponent(qrcode))
}
bot.on('scan', onScan)
await bot.start()
from wechaty import Wechaty, ScanStatus
from typing import Optional
import asyncio
# method one
def on_scan (qrcode, status):
print('Scan QR Code to login: {}\n'.format(staus))
print('https://wechaty.js.org/qrcode/{}'.format(qrcode))
bot = Wechaty()
bot.on('scan', on_scan)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_scan(self, qr_code: str, status: ScanStatus, data: Optional[str]):
"""listen scan event"""
print('Scan QR Code to login: {}\n'.format(staus))
print('https://wechaty.js.org/qrcode/{}'.format(qrcode))
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
login
Event: bot contact
TODO: introducing login
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
import { Contact } from 'wechaty'
function onLogin (bot: Contact) {
console.info('Bot logged in:', bot)
}
bot.on('login', onLogin)
await bot.start()
function onLogin (bot) {
console.info('Bot logged in:', bot)
}
bot.on('login', onLogin)
await bot.start()
from wechaty import Wechaty, Contact
from typing import Optional
import asyncio
# method one
def on_login (contact: Contact):
print(f'User {contact} logged in\n')
bot.on('login', on_login)
# method two (suggested)
class MyBot(Wechaty):
async def on_login(self, contact: Contact):
print(f'User {contact} logged in\n')
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
logout
Event
TODO: introducing logout
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
from wechaty import Wechaty, Contact
import asyncio
# method one
async def on_logout(contact: Contact):
print(f'User <{contact}> logout')
bot = MyBot()
bot.on('logout', on_logout)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_logout(self, contact: Contact):
print(f'User <{contact}> logout')
async def start():
await MyBot().start()
asyncio.run(start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
message
Event
TODO: introducing message
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
import { Message } from 'wechaty'
function onMessage (message: Message) {
console.info('New message:', message)
}
bot.on('message', onMessage)
await bot.start()
function onMessage (message) {
console.info('New message:', message)
}
bot.on('message', onMessage)
await bot.start()
from wechaty import Wechaty, Message
import asyncio
# method one
async def on_message(msg: Message):
print(f'receive message <{msg}>')
bot = MyBot()
bot.on('message', on_message)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_message(self, msg: Message):
print(f'receive message <{msg}>')
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
friendship
Event: friend requests
TODO: introducing friendship
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
from wechaty import Wechaty, Friendship
import asyncio
# method one
async def on_friendship(friendship: Friendship):
print(f'receive friendship<{friendship}> event')
bot = MyBot()
bot.on('friendship', on_friendship)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_friendship(self, friendship: Friendship):
print(f'receive friendship<{friendship}> event')
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
Room Events
room-topic
Event: messages
TODO: introducing room-topic
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
from wechaty import Wechaty, Room, Contact
from datetime import datetime
import asyncio
# method one
async def on_room_topic(room: Room, new_topic: str, old_topic: str, changer: Contact, date: datetime):
print(f'receive room topic changed event <from<{new_topic}> to <{old_topic}>> from room<{room}> ')
bot = MyBot()
bot.on('room-topic', on_room_topic)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_room_topic(self, room: Room, new_topic: str, old_topic: str, changer: Contact, date: datetime):
print(f'receive room topic changed event <from<{new_topic}> to <{old_topic}>> from room<{room}> ')
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
room-invite
Event: messages
TODO: introducing room-invite
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
from wechaty import Wechaty, RoomInvitation
import asyncio
# method one
async def on_room_invite(room_invitation: RoomInvitation):
print(f'receive room invitation<{room_invitation}> event')
bot = MyBot()
bot.on('room-invite', on_room_invite)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_room_invite(self, room_invitation: RoomInvitation):
print(f'receive room invitation<{room_invitation}> event')
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
room-join
Event: messages
TODO: introducing room-join
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
from wechaty import Wechaty, Contact, Room
from typing import List
from datetime import datetime
import asyncio
# method one
async def on_room_join(room: Room, invitees: List[Contact], inviter: Contact, date: datetime):
print(f'receive room join event from Room<{room}>')
bot = Wechaty()
bot.on('room-join', on_room_join)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_room_join(self, room: Room, invitees: List[Contact], inviter: Contact, date: datetime):
print(f'receive room join event from Room<{room}>')
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
room-leave
Event: messages
TODO: introducing room-leave
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
from wechaty import Wechaty, Contact, Room
from typing import List
from datetime import datetime
import asyncio
# method one
async def on_room_leave(room: Room, leavers: List[Contact], remover: Contact, date: datetime):
print(f'receive room leave event from Room<{room}>')
bot = Wechaty()
bot.on('room-leave', on_room_leave)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_room_leave(self, room: Room, leavers: List[Contact], remover: Contact, date: datetime):
print(f'receive room leave event from Room<{room}>')
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
System events
ready
Event
TODO: introducing ready
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
from wechaty import Wechaty
from wechaty_puppet import EventReadyPayload
import asyncio
# method one
async def on_ready(payload: EventReadyPayload):
"""Any initialization work can be put in here
Args:
payload (EventReadyPayload): ready data
"""
print(f'receive ready event<{payload}>')
bot = Wechaty()
bot.on('ready', on_ready)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_ready(self, payload: EventReadyPayload):
"""Any initialization work can be put in here
Args:
payload (EventReadyPayload): ready data
"""
print(f'receive ready event<{payload}>')
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
heartbeat
Event: messages
TODO: introducing heartbeat
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
from wechaty import Wechaty
from wechaty_puppet import EventHeartbeatPayload
import asyncio
# method one
async def on_heartbeat(payload: EventHeartbeatPayload):
print(f'receive heartbeat event from server <{payload}>')
bot = Wechaty()
bot.on('heartbeat', on_heartbeat)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_heartbeat(self, payload: EventHeartbeatPayload):
print(f'receive heartbeat event from server <{payload}>')
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
error
Event
TODO: introducing error
event
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
from wechaty import Wechaty
from wechaty_puppet import EventErrorPayload
import asyncio
# method one
async def on_error(payload: EventErrorPayload):
print(f'receive error event<{payload}> from sever')
bot = Wechaty()
bot.on('error', on_error)
asyncio.run(bot.start())
# method two (suggested)
class MyBot(Wechaty):
async def on_error(self, payload: EventErrorPayload):
print(f'receive error event<{payload}> from sever')
asyncio.run(MyBot().start())
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!