Skip to content

add message file bot #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,8 @@ dmypy.json

# Pyre type checker
.pyre/
token.txt
token.txt
.idea/
logs/
**/logs/
**/log.txt
99 changes: 99 additions & 0 deletions examples/advanced/message-file-bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""send contact card to specific contact"""
# pylint: disable=R0801
import asyncio
import logging
from typing import Optional, Union

from wechaty_puppet import FileBox, ScanStatus # type: ignore
from wechaty_puppet import MessageType

from wechaty import Wechaty, Contact
from wechaty.user import Message, Room

logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)


class MyBot(Wechaty):
"""
listen wechaty event with inherited functions, which is more friendly for
oop developer
"""
def __init__(self):
super().__init__()

async def on_message(self, msg: Message):
"""
listen for message event
"""
from_contact = msg.talker()
text = msg.text()
room = msg.room()
if room:
await room.ready()

# send contact-card
if msg.type() == MessageType.MESSAGE_TYPE_CONTACT:
# we can receive the contact-card event, and get the contact from message
contact = await msg.to_contact()

if text == 'send card':
# find one of my friend to send to `from_contact`
contacts = await bot.Contact.find_all()
if contacts:
# send one of my friend to the talker
# !! this interface is not supported now
await from_contact.say(contacts[0])
print('have sended')
elif msg.type() == MessageType.MESSAGE_TYPE_IMAGE:
img = await msg.to_file_box()
# save the image as local file
await img.to_file(f'./{img.name}')
# send image file to the room
if room:
await room.say(img)

elif msg.type() == MessageType.MESSAGE_TYPE_VIDEO:
video = await msg.to_file_box()
# save the video as local file
await video.to_file(f'./{video.name}')

# send video file to the room
if room:
await room.say(video)

elif msg.type() == MessageType.MESSAGE_TYPE_AUDIO:
audio = await msg.to_file_box()
# save the audio file as local file
await audio.to_file(f'./{audio.name}')
# !! we can't send audio to room/contact

print('done')

async def on_login(self, contact: Contact):
"""login event. It will be triggered every time you login"""
log.info(f'user: {contact} has login')

async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
data: Optional[str] = None):
"""scan event, It will be triggered when you scan the qrcode to login.
And it will not be triggered when you have logined
"""
contact = self.Contact.load(self.contact_id)
await contact.ready()
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')


bot: Optional[MyBot] = None


async def main():
"""doc"""
# pylint: disable=W0603
global bot
bot = MyBot()
await bot.start()


asyncio.run(main())
76 changes: 76 additions & 0 deletions examples/advanced/mini-program-bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""send contact card to specific contact"""
# pylint: disable=R0801
import asyncio
import logging
from typing import Optional, Union
import json
from dataclasses import asdict

from wechaty_puppet import FileBox, ScanStatus # type: ignore
from wechaty_puppet import MessageType

from wechaty import Wechaty, Contact
from wechaty.user import Message, Room

logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)


class MyBot(Wechaty):
"""
listen wechaty event with inherited functions, which is more friendly for
oop developer
"""
def __init__(self):
super().__init__()

async def on_message(self, msg: Message):
"""
listen for message event
"""
from_contact = msg.talker()
text = msg.text()

room = self.Room.load('19961884194@chatroom')
await room.ready()

if msg.type() == MessageType.MESSAGE_TYPE_MINI_PROGRAM:
mini_program = await msg.to_mini_program()

# save the mini-program data as string
mini_program_data = asdict(mini_program.payload)

# load the min-program
loaded_mini_program = self.MiniProgram.create_from_json(
payload_data=mini_program_data
)

await room.say(loaded_mini_program)

async def on_login(self, contact: Contact):
"""login event. It will be triggered every time you login"""
log.info(f'user: {contact} has login')

async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
data: Optional[str] = None):
"""scan event, It will be triggered when you scan the qrcode to login.
And it will not be triggered when you have logined
"""
contact = self.Contact.load(self.contact_id)
await contact.ready()
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')


bot: Optional[MyBot] = None


async def main():
"""doc"""
# pylint: disable=W0603
global bot
bot = MyBot()
await bot.start()


asyncio.run(main())
2 changes: 1 addition & 1 deletion examples/ding-dong-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def on_message(msg: Message):
await msg.say(file_box)


async def on_scan(qrcode: str, status: int):
async def on_scan(qrcode: str, status: int, data):
"""
Scan Handler for the Bot
"""
Expand Down