Skip to content

Commit

Permalink
:sparkling: #8 初步调整支持java调用,因为scala的枚举在java调用产生问题,所以需要调整schemas的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
jcai committed Jun 4, 2020
1 parent 48f6382 commit 4b80f07
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 25 deletions.
40 changes: 40 additions & 0 deletions wechaty/src/main/java/wechaty/JavaDingDongBot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package wechaty;

import wechaty.puppet.schemas.Message;

import static jdk.nashorn.internal.objects.Global.println;

/**
* @author <a href="mailto:[email protected]">Jun Tsai</a>
* @since 2020-06-04
*/
public class JavaDingDongBot {
public static void main(String[] args) throws InterruptedException {
WechatyOptions option = new WechatyOptions();
Wechaty bot = Wechaty.instance(option);
bot
.onScan(payload -> {
System.out.println(payload);
System.out.println(String.format("Scan QR Code to login: %s\nhttps://api.qrserver.com/v1/create-qr-code/?data=%s\n",payload.status().toString(), payload.qrcode()));
})
.onLogin(payload -> {
System.out.println(String.format("User %s logined",payload.id()));
})
.onMessage(message -> {
System.out.println(message.payload().type());
// if (message.payload().type() != Message.MessageType.MessageTypeText() || !message.payload().text().equals("#ding")){
if (!message.payload().text().equals("#ding")){
System.out.println("Message discarded because it does not match #ding");
}else{
System.out.println("send message to "+ message.payload().fromId());
message.say("dong");
System.out.println("dong");
}
});


bot.start();

Thread.currentThread().join();
}
}
31 changes: 21 additions & 10 deletions wechaty/src/main/scala/wechaty/Wechaty.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package wechaty

import java.util.function.Consumer

import wechaty.Wechaty.PuppetResolver
import wechaty.helper.ImplicitHelper._
import wechaty.hostie.PuppetHostie
import wechaty.puppet.{LoggerSupport, Puppet}
import wechaty.puppet.events.EventEmitter
import wechaty.puppet.schemas.Events._
import wechaty.puppet.schemas.Puppet.PuppetOptions
import wechaty.puppet.LoggerSupport
import wechaty.user.{Contact, Message}

import scala.language.implicitConversions

import scala.language.implicitConversions;

/**
**
Expand Down Expand Up @@ -40,6 +42,9 @@ object Wechaty {
if (globalInstance == null) globalInstance = new Wechaty(options)
globalInstance
}
trait PuppetResolver{
def puppet:Puppet
}
}

class WechatyOptions {
Expand All @@ -49,26 +54,29 @@ class WechatyOptions {
var ioToken: Option[String] = None
}

class Wechaty(private val options: WechatyOptions) extends LoggerSupport{
private implicit var hostie:PuppetHostie = _
class Wechaty(private val options: WechatyOptions) extends LoggerSupport with PuppetResolver{
private var hostie:PuppetHostie = _
private implicit val puppetResolver: PuppetResolver = this

def onScan(listener: EventScanPayload=>Unit): Wechaty = {
EventEmitter.addListener(PuppetEventName.SCAN,listener)
def onScan(listener: Consumer[EventScanPayload]): Wechaty = {
EventEmitter.addListener[EventScanPayload](PuppetEventName.SCAN,listener)
this
}
def onLogin(listener:Contact =>Unit):Wechaty={
def onLogin(listener:Consumer[Contact]):Wechaty={
EventEmitter.addListener[EventLoginPayload](PuppetEventName.LOGIN,listener)
this
}
def onMessage(listener:Message =>Unit):Wechaty={
def onMessage(listener:Consumer[Message]):Wechaty={
EventEmitter.addListener[EventMessagePayload](PuppetEventName.MESSAGE,listener)
this
}
def onLogout(listener:Contact=>Unit):Wechaty={
def onLogout(listener:Consumer[Contact]):Wechaty={
EventEmitter.addListener[EventLogoutPayload](PuppetEventName.LOGOUT,listener)
this
}

override def puppet: Puppet = this.hostie

def start(): Unit = {
val option = options.puppetOptions match{
case Some(o) => o
Expand All @@ -84,6 +92,9 @@ class Wechaty(private val options: WechatyOptions) extends LoggerSupport{
}))

}
// implicit def toScalaUnit(listener:Contact=>Unit):java.util.function.Function[Contact,Void]={
//
// }
}


20 changes: 13 additions & 7 deletions wechaty/src/main/scala/wechaty/helper/ImplicitHelper.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package wechaty.helper

import java.util.function.Consumer

import wechaty.Wechaty.PuppetResolver
import wechaty.puppet.Puppet
import wechaty.puppet.schemas.Events.{EventLoginPayload, EventLogoutPayload, EventMessagePayload}
import wechaty.puppet.schemas.Events.{EventLoginPayload, EventLogoutPayload, EventMessagePayload, EventScanPayload}
import wechaty.user.{Contact, Message}

import scala.language.implicitConversions
Expand All @@ -12,16 +15,19 @@ import scala.language.implicitConversions
* @since 2020-06-04
*/
object ImplicitHelper {
private[wechaty] implicit def toMessage(messageListener: Message => Unit)(implicit puppet: Puppet): EventMessagePayload => Unit = {
messagePayload: EventMessagePayload => messageListener(new Message(messagePayload.messageId))
private[wechaty] implicit def toEventScanPayload(eventScanListener: Consumer[EventScanPayload])(implicit puppet: PuppetResolver): EventScanPayload=> Unit = {
eventScanPayload: EventScanPayload => { eventScanListener.accept(eventScanPayload) }
}
private[wechaty] implicit def toMessage(messageListener: Consumer[Message])(implicit puppet: PuppetResolver): EventMessagePayload => Unit = {
messagePayload: EventMessagePayload => { messageListener.accept(new Message(messagePayload.messageId)) }
}

private[wechaty] implicit def toContact(contactListener: Contact => Unit)(implicit puppet: Puppet): EventLoginPayload => Unit = {
payload: EventLoginPayload => contactListener(new Contact(payload.contactId))
private[wechaty] implicit def toContact(contactListener: Consumer[Contact])(implicit puppet: PuppetResolver): EventLoginPayload => Unit = {
payload: EventLoginPayload => { contactListener.accept(new Contact(payload.contactId)) }
}

private[wechaty] implicit def logoutToContact(contactListener: Contact => Unit)(implicit puppet: Puppet): EventLogoutPayload => Unit = {
payload: EventLogoutPayload => contactListener(new Contact(payload.contactId))
private[wechaty] implicit def logoutToContact(contactListener: Consumer[Contact])(implicit puppet: PuppetResolver): EventLogoutPayload => Unit = {
payload: EventLogoutPayload => { contactListener.accept(new Contact(payload.contactId)) }
}

}
6 changes: 3 additions & 3 deletions wechaty/src/main/scala/wechaty/user/Contact.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package wechaty.user

import wechaty.puppet.Puppet
import wechaty.Wechaty.PuppetResolver
import wechaty.puppet.schemas

/**
*
* @author <a href="mailto:[email protected]">Jun Tsai</a>
* @since 2020-06-03
*/
class Contact(contactId:String)(implicit puppet:Puppet) {
lazy val payload: schemas.Contact.ContactPayload = puppet.contactPayload(contactId)
class Contact(contactId:String)(implicit resolver:PuppetResolver) {
lazy val payload: schemas.Contact.ContactPayload = resolver.puppet.contactPayload(contactId)
//delegate method
def id=contactId
def name = payload.name
Expand Down
10 changes: 5 additions & 5 deletions wechaty/src/main/scala/wechaty/user/Message.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package wechaty.user

import wechaty.puppet.Puppet
import wechaty.Wechaty.PuppetResolver
import wechaty.puppet.schemas
import wechaty.puppet.schemas.Puppet

Expand All @@ -9,10 +9,10 @@ import wechaty.puppet.schemas.Puppet
* @author <a href="mailto:[email protected]">Jun Tsai</a>
* @since 2020-06-02
*/
class Message(messageId:String)(implicit puppet:Puppet) {
lazy val payload: schemas.Message.MessagePayload = puppet.messagePayload(messageId)
class Message(messageId:String)(implicit resolver: PuppetResolver) {
lazy val payload: schemas.Message.MessagePayload = resolver.puppet.messagePayload(messageId)
def say(text:String): Unit = {
puppet.messageSendText(sayId(),text)
resolver.puppet.messageSendText(sayId(),text)
}
private def sayId(): String ={
if(!Puppet.isBlank(payload.roomId)) payload.roomId
Expand All @@ -21,6 +21,6 @@ class Message(messageId:String)(implicit puppet:Puppet) {
}

override def toString: String = {
payload.text
if(payload!= null ) payload.text else ""
}
}

0 comments on commit 4b80f07

Please sign in to comment.