1
1
package wechaty
2
2
3
+ import wechaty .helper .ImplicitHelper ._
3
4
import wechaty .hostie .PuppetHostie
4
5
import wechaty .puppet .events .EventEmitter
5
- import wechaty .puppet .schemas .Events .{ EventLoginPayload , EventMessagePayload , EventName , EventScanPayload }
6
+ import wechaty .puppet .schemas .Events ._
6
7
import wechaty .puppet .schemas .Puppet .PuppetOptions
7
- import wechaty .puppet .{ LoggerSupport , PuppetOption }
8
+ import wechaty .puppet .LoggerSupport
8
9
import wechaty .user .{Contact , Message }
9
10
10
11
import scala .language .implicitConversions
11
12
12
13
13
14
/**
15
+ **
16
+ * Main bot class.
14
17
*
18
+ * A `Bot` is a WeChat client depends on which puppet you use.
19
+ *
20
+ * See more:
21
+ * - [What is a Puppet in Wechaty](https://github.com/wechaty/wechaty-getting-started/wiki/FAQ-EN#31-what-is-a-puppet-in-wechaty)
22
+ *
23
+ * > If you want to know how to send message, see [Message](#Message) <br>
24
+ * > If you want to know how to get contact, see [Contact](#Contact)
25
+ *
26
+ * @example <caption>The World's Shortest ChatBot Code: 6 lines of Scala</caption>
27
+ * val options = new WechaytOptions
28
+ * val bot = Wechaty.instance(options)
29
+ * bot.onScan(payload => println("['https://api.qrserver.com/v1/create-qr-code/?data=',encodeURIComponent(qrcode),'&size=220x220&margin=20',].join('')"))
30
+ * bot.onLogin(user => println("User ${user} logged in"))
31
+ * bot.onMessage(message => println("Message: ${message}"))
32
+ * bot.start()
15
33
* @author <a href="mailto:[email protected] ">Jun Tsai</a>
16
34
* @since 2020-06-01
17
35
*/
18
36
object Wechaty {
37
+ private var globalInstance : Wechaty = _
19
38
def instance (options : WechatyOptions ): Wechaty = {
20
- new Wechaty (options)
39
+ if (options != null && globalInstance != null ) throw new Error (" instance can be only initialized once by options !" )
40
+ if (globalInstance == null ) globalInstance = new Wechaty (options)
41
+ globalInstance
21
42
}
22
43
}
23
44
@@ -28,28 +49,31 @@ class WechatyOptions {
28
49
var ioToken : Option [String ] = None
29
50
}
30
51
31
- class Wechaty (options : WechatyOptions ) extends LoggerSupport {
52
+ class Wechaty (private val options : WechatyOptions ) extends LoggerSupport {
32
53
private implicit var hostie : PuppetHostie = _
33
54
34
55
def onScan (listener : EventScanPayload => Unit ): Wechaty = {
35
- EventEmitter .addListener(EventName . PuppetEventNameScan ,listener)
56
+ EventEmitter .addListener(PuppetEventName . SCAN ,listener)
36
57
this
37
58
}
38
59
def onLogin (listener: Contact => Unit ): Wechaty = {
39
- EventEmitter .addListener[EventLoginPayload ](EventName . PuppetEventNameLogin ,listener)
60
+ EventEmitter .addListener[EventLoginPayload ](PuppetEventName . LOGIN ,listener)
40
61
this
41
62
}
42
63
def onMessage (listener: Message => Unit ): Wechaty = {
43
- EventEmitter .addListener[EventMessagePayload ](EventName . PuppetEventNameMessage ,listener)
64
+ EventEmitter .addListener[EventMessagePayload ](PuppetEventName . MESSAGE ,listener)
44
65
this
45
66
}
46
- def onLogout (listener: EventLoginPayload => Unit ): Wechaty = {
47
- EventEmitter .addListener( EventName . PuppetEventNameLogout ,listener)
67
+ def onLogout (listener: Contact => Unit ): Wechaty = {
68
+ EventEmitter .addListener[ EventLogoutPayload ]( PuppetEventName . LOGOUT ,listener)
48
69
this
49
70
}
50
71
51
72
def start (): Unit = {
52
- val option = new PuppetOption
73
+ val option = options.puppetOptions match {
74
+ case Some (o) => o
75
+ case _ => new PuppetOptions
76
+ }
53
77
this .hostie = new PuppetHostie (option)
54
78
this .hostie.start()
55
79
Runtime .getRuntime.addShutdownHook(new Thread (new Runnable {
@@ -60,12 +84,6 @@ class Wechaty(options: WechatyOptions) extends LoggerSupport{
60
84
}))
61
85
62
86
}
63
- implicit def toMessage (messageListener : Message => Unit ): EventMessagePayload => Unit = {
64
- messagePayload: EventMessagePayload => messageListener(new Message (messagePayload.messageId))
65
- }
66
- implicit def toContact (contactListener : Contact => Unit ): EventLoginPayload => Unit = {
67
- payload: EventLoginPayload => contactListener(new Contact (payload.contactId))
68
- }
69
87
}
70
88
71
89
0 commit comments