Skip to content
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

fix:监听扫码事件参数对应错误以及格式化 #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 13 additions & 9 deletions examples/advanced/busy-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
import logging
from typing import Optional

# waiting for wechaty-puppet new version
from wechaty_puppet import ScanStatus # type: ignore

from wechaty import (
Wechaty, Contact, Message
)
# waiting for wechaty-puppet new version
from wechaty_puppet import ScanStatus # type: ignore

logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
Expand All @@ -33,6 +32,7 @@ class MyBot(Wechaty):
listen wechaty event with inherited functions, which is more friendly for
oop developer
"""

def __init__(self):
super().__init__()
self.busy = False
Expand All @@ -56,20 +56,24 @@ async def on_message(self, msg: Message):
elif text == '#free':
await from_contact.say('auto reply stopped.')
elif text == '#busy':
self.busy= True
self.busy = True
await from_contact.say('auto reply enabled')
else:
await from_contact.say(self.auto_reply_comment)


async def on_login(self, contact: Contact):
print(f'user: {contact} has login')

async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
async def on_scan(self,
qr_code: str,
status: ScanStatus,
data: Optional[str] = None):
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')
if status == ScanStatus.Waiting:
print("qr_code: ", "https://wechaty.js.org/qrcode/" + qr_code)
else:
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')


bot: Optional[MyBot] = None
Expand Down
13 changes: 9 additions & 4 deletions examples/advanced/gif-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,16 @@ async def on_message(self, msg: Message):
async def on_login(self, contact: Contact):
print(f'user: {contact} has login')

async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
async def on_scan(self,
qr_code: str,
status: ScanStatus,
data: Optional[str] = None):
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')
if status == ScanStatus.Waiting:
print("qr_code: ", "https://wechaty.js.org/qrcode/" + qr_code)
else:
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')


async def main():
Expand Down
15 changes: 9 additions & 6 deletions examples/advanced/message-file-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,19 @@ 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,
async def on_scan(self,
qr_code: str,
status: ScanStatus,
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}')

if status == ScanStatus.Waiting:
print("qr_code: ", "https://wechaty.js.org/qrcode/" + qr_code)
else:
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')

bot: Optional[MyBot] = None

Expand Down
14 changes: 9 additions & 5 deletions examples/advanced/mini-program-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,19 @@ 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,
async def on_scan(self,
qr_code: str,
status: ScanStatus,
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}')
if status == ScanStatus.Waiting:
print("qr_code: ", "https://wechaty.js.org/qrcode/" + qr_code)
else:
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')


bot: Optional[MyBot] = None
Expand Down
21 changes: 15 additions & 6 deletions examples/advanced/room_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import re
import time
from datetime import datetime
from typing import List
from typing import List, Optional

from apscheduler.schedulers.asyncio import AsyncIOScheduler

from wechaty import Contact, Room, Wechaty, get_logger, Message
from wechaty_puppet import ScanStatus

welcome = """=============== Powered by Python-Wechaty ===============
-------- https://github.com/Chatie/python-wechaty --------
Expand Down Expand Up @@ -129,10 +129,19 @@ async def create_ding_room(bot, contact):

class MyBot(Wechaty):

# def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
# data: Optional[str] = None):
# qr_terminal(qr_code, 1)
# log.info("{0}\n[{1}] Scan QR Code in above url to login: ".format(qr_code, status))
async def on_scan(self,
qr_code: str,
status: ScanStatus,
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
"""
if status == ScanStatus.Waiting:
print("qr_code: ", "https://wechaty.js.org/qrcode/" + qr_code)
else:
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')

def on_error(self, payload):
log.info(str(payload))
Expand Down
17 changes: 11 additions & 6 deletions examples/basic/contact-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import logging
from typing import Optional

from wechaty_puppet import ScanStatus, ContactType # type: ignore

from wechaty import Wechaty, Contact
from wechaty_puppet import ScanStatus, ContactType # type: ignore

logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
Expand All @@ -16,6 +15,7 @@ class MyBot(Wechaty):
listen wechaty event with inherited functions, which is more friendly for
oop developer
"""

def __init__(self):
super().__init__()

Expand Down Expand Up @@ -46,11 +46,16 @@ async def on_login(self, contact: Contact):
if contact.payload.alias == 'lover':
await contact.say('my love')

async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
async def on_scan(self,
qr_code: str,
status: ScanStatus,
data: Optional[str] = None):
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')
if status == ScanStatus.Waiting:
print("qr_code: ", "https://wechaty.js.org/qrcode/" + qr_code)
else:
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')


bot: Optional[MyBot] = None
Expand Down
18 changes: 11 additions & 7 deletions examples/basic/ding-dong-bot-oop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
# pylint: disable=R0801
import asyncio
import logging
import os
from typing import Optional, Union

from wechaty_puppet import FileBox, ScanStatus # type: ignore

from wechaty import Wechaty, Contact
from wechaty.user import Message, Room
from wechaty_puppet import FileBox, ScanStatus # type: ignore

logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
Expand All @@ -19,6 +17,7 @@ class MyBot(Wechaty):
listen wechaty event with inherited functions, which is more friendly for
oop developer
"""

def __init__(self):
super().__init__()

Expand All @@ -43,11 +42,16 @@ async def on_message(self, msg: Message):
async def on_login(self, contact: Contact):
print(f'user: {contact} has login')

async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
async def on_scan(self,
qr_code: str,
status: ScanStatus,
data: Optional[str] = None):
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')
if status == ScanStatus.Waiting:
print("qr_code: ", "https://wechaty.js.org/qrcode/" + qr_code)
else:
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')


bot: Optional[MyBot] = None
Expand Down
25 changes: 12 additions & 13 deletions examples/ding-dong-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import asyncio

from urllib.parse import quote
import os
from typing import Optional

from wechaty import (
Contact,
Expand All @@ -43,15 +42,15 @@ async def on_message(msg: Message):


async def on_scan(
qrcode: str,
status: ScanStatus,
_data,
qr_code: str,
status: ScanStatus,
data: Optional[str] = None
):
"""
Scan Handler for the Bot
"""
print('Status: ' + str(status))
print('View QR Code Online: https://wechaty.js.org/qrcode/' + quote(qrcode))
if status == ScanStatus.Waiting:
print("scan status: ", status.name, "qr_code: ", "https://wechaty.js.org/qrcode/" + qr_code)


async def on_login(user: Contact):
Expand All @@ -74,11 +73,11 @@ async def main():
# Those types of puppet_service are supported natively.
# https://wechaty.js.org/docs/puppet-services/paimon
# https://wechaty.js.org/docs/puppet-services/wxwork
#
#
# Replace your token here and umcommt that line, you can just run this python file successfully!
# os.environ['token'] = 'puppet_paimon_your_token'
# os.environ['token'] = 'puppet_wxwork_your_token'
#
#
if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ:
print('''
Error: WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables
Expand All @@ -88,9 +87,9 @@ async def main():

bot = Wechaty()

bot.on('scan', on_scan)
bot.on('login', on_login)
bot.on('message', on_message)
bot.on('scan', on_scan)
bot.on('login', on_login)
bot.on('message', on_message)

await bot.start()

Expand Down