diff --git a/examples/advanced/busy-bot.py b/examples/advanced/busy-bot.py index fb9c431..965e294 100644 --- a/examples/advanced/busy-bot.py +++ b/examples/advanced/busy-bot.py @@ -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__) @@ -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 @@ -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 diff --git a/examples/advanced/gif-bot.py b/examples/advanced/gif-bot.py index df08e9e..31d3a93 100644 --- a/examples/advanced/gif-bot.py +++ b/examples/advanced/gif-bot.py @@ -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(): diff --git a/examples/advanced/message-file-bot.py b/examples/advanced/message-file-bot.py index ddfdd76..7a977c4 100644 --- a/examples/advanced/message-file-bot.py +++ b/examples/advanced/message-file-bot.py @@ -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 diff --git a/examples/advanced/mini-program-bot.py b/examples/advanced/mini-program-bot.py index 5666d9d..559279a 100644 --- a/examples/advanced/mini-program-bot.py +++ b/examples/advanced/mini-program-bot.py @@ -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 diff --git a/examples/advanced/room_bot.py b/examples/advanced/room_bot.py index 4cf2479..b77d78e 100644 --- a/examples/advanced/room_bot.py +++ b/examples/advanced/room_bot.py @@ -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 -------- @@ -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)) diff --git a/examples/basic/contact-bot.py b/examples/basic/contact-bot.py index de2246c..26b7960 100644 --- a/examples/basic/contact-bot.py +++ b/examples/basic/contact-bot.py @@ -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__) @@ -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__() @@ -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 diff --git a/examples/basic/ding-dong-bot-oop.py b/examples/basic/ding-dong-bot-oop.py index dd903c3..276c296 100644 --- a/examples/basic/ding-dong-bot-oop.py +++ b/examples/basic/ding-dong-bot-oop.py @@ -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__) @@ -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__() @@ -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 diff --git a/examples/ding-dong-bot.py b/examples/ding-dong-bot.py index f288539..20c8e75 100755 --- a/examples/ding-dong-bot.py +++ b/examples/ding-dong-bot.py @@ -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, @@ -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): @@ -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 @@ -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()