Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Using Hooks to start

YinMo edited this page May 26, 2022 · 13 revisions

About Hooks

More convenient to manage all commands and events

Make First

1. The first step, import the CHLINE client

from CHRLINE import *
from CHRLINE.hooks import HooksTracer

2. Instantiate the client

cl = CHRLINE(useThrift=True)

You will get a Url and Qrcode picture, login it on your LINE mobile

3. Instantiate the hooks

tracer = HooksTracer(
    cl,  # main account
    prefixes=["/"],  # cmd prefixes
)

use HooksTracer to create a new Hooks

4. Instantiate the detect Operation Event

class OpHook(object):
    @tracer.Operation(26)
    def receiveMessage(self, op, cl):
        msg = op.message
        self.trace(msg, self.HooksType["Content"], cl)

create a new event in Hook, when event detect, excute bind function

ex. Operation 26 is detect receive messages, excute receiveMessage

5. Instantiate to detect Content Event

class ContentHook(object):
    @tracer.Content(0)
    def TextMessage(self, msg, cl):
        text = msg.text
        self.trace(msg, self.HooksType['Command'], cl)

create a new Message content event in Hook, when event detect, excute bind function

ex. Content 0 is text message, excute function TextMessage

6. Instantiate the detect Command Event

class NormalCmd(object):
    @tracer.Command(ignoreCase=True)
    def hi(self, msg, cl):
        cl.replyMessage(msg, "Hi!")

create a new Command event in Hook, when event detect, excute bind function

ex. message is /hi,bot can excute function hi to reply you Hi!

7. Instantiate the trace bot

tracer.run()

Code preview

from CHRLINE import *
from CHRLINE.hooks import HooksTracer

cl = CHRLINE(useThrift=True)

tracer = HooksTracer(
    cl,  # main account
    prefixes=["/"],  # cmd prefixes
)

class OpHook(object):
    @tracer.Operation(26)
    def receiveMessage(self, op, cl):
        msg = op.message
        self.trace(msg, self.HooksType["Content"], cl)

class ContentHook(object):
    @tracer.Content(0)
    def TextMessage(self, msg, cl):
        text = msg.text
        self.trace(msg, self.HooksType['Command'], cl)

class NormalCmd(object):
    @tracer.Command(ignoreCase=True)
    def hi(self, msg, cl):
        cl.replyMessage(msg, "Hi!")

tracer.run()