-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
417 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package lxlib | ||
|
||
import ( | ||
"github.com/lxbot/lxlib/v2/common" | ||
"github.com/lxbot/lxlib/v2/lxtypes" | ||
) | ||
|
||
type Adapter struct { | ||
common *common.LxCommon | ||
eventCh *chan *lxtypes.Event | ||
messageCh *chan *lxtypes.Message | ||
} | ||
|
||
func NewAdapter() (*Adapter, *chan *lxtypes.Message) { | ||
messageCh := make(chan *lxtypes.Message) | ||
eventCh := make(chan *lxtypes.Event) | ||
|
||
common := common.NewLxCommon() | ||
adapter := &Adapter{ | ||
common: common, | ||
eventCh: &eventCh, | ||
messageCh: &messageCh, | ||
} | ||
|
||
go common.Listen(&eventCh) | ||
go adapter.listen() | ||
adapter.Raw(lxtypes.NewEvent(lxtypes.ReadyEvent, nil)) | ||
|
||
return adapter, &messageCh | ||
} | ||
|
||
func (this *Adapter) listen() { | ||
for { | ||
eventPtr := <-*this.eventCh | ||
switch eventPtr.Event { | ||
case lxtypes.OutgoingMessageEvent: | ||
*this.messageCh <- eventPtr.Payload.(*lxtypes.Message) | ||
} | ||
} | ||
} | ||
|
||
func (this *Adapter) Raw(event *lxtypes.Event) { | ||
go this.common.Send(event) | ||
} | ||
|
||
func (this *Adapter) Send(message *lxtypes.Message) { | ||
go this.common.Send(lxtypes.NewEvent(lxtypes.IncomingMessageEvent, message)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package common | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
|
||
"github.com/lxbot/lxlib/v2/lxtypes" | ||
) | ||
|
||
type LxCommon struct { | ||
logger *log.Logger | ||
} | ||
|
||
func NewLxCommon() *LxCommon { | ||
return &LxCommon{ | ||
logger: log.New(os.Stdout, "", 0), | ||
} | ||
} | ||
|
||
func (this *LxCommon) Listen(event *chan *lxtypes.Event) { | ||
d := json.NewDecoder(os.Stdin) | ||
var data map[string]interface{} | ||
|
||
for { | ||
err := d.Decode(&data) | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} else { | ||
fmt.Fprintln(os.Stderr, err) | ||
continue | ||
} | ||
} | ||
message := lxtypes.NewEvent(lxtypes.IncomingMessageEvent, data) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
} | ||
*event <- message | ||
} | ||
} | ||
|
||
func (this *LxCommon) Send(message *lxtypes.Event) { | ||
m, err := ToMap(message) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
} | ||
this.logger.Println(m) | ||
} | ||
|
||
func (this *LxCommon) Close() { | ||
message := lxtypes.NewEvent(lxtypes.CloseEvent, nil) | ||
m, err := ToMap(message) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
} | ||
this.logger.Println(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package common | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/lxbot/lxlib/v2/lxtypes" | ||
) | ||
|
||
func ToMap(i interface{}) (lxtypes.M, error) { | ||
t, err := json.Marshal(i) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var r lxtypes.M | ||
if err := json.Unmarshal(t, &r); err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
module github.com/lxbot/lxlib | ||
module github.com/lxbot/lxlib/v2 | ||
|
||
go 1.13 | ||
|
||
require github.com/go-test/deep v1.0.5 | ||
require ( | ||
github.com/go-test/deep v1.0.5 | ||
github.com/rs/xid v1.3.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/go-test/deep v1.0.5 h1:AKODKU3pDH1RzZzm6YZu77YWtEAq6uh1rLIAQlay2qc= | ||
github.com/go-test/deep v1.0.5/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8= | ||
github.com/rs/xid v1.3.0 h1:6NjYksEUlhurdVehpc7S7dk6DAmcKv8V9gG0FsVN2U4= | ||
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package lxtypes | ||
|
||
import "github.com/rs/xid" | ||
|
||
type ( | ||
Event struct { | ||
ID string `json:"id"` | ||
Event EventType `json:"event"` | ||
Payload interface{} `json:"payload"` | ||
} | ||
EventType string | ||
) | ||
|
||
const ( | ||
ReadyEvent EventType = "ready" | ||
CloseEvent = "close" | ||
IncomingMessageEvent = "outgoing_message" | ||
OutgoingMessageEvent = "incoming_message" | ||
GetStorageEvent = "get_storage" | ||
SetStorageEvent = "set_storage" | ||
) | ||
|
||
func NewEvent(event EventType, payload interface{}) *Event { | ||
return &Event{ | ||
ID: xid.New().String(), | ||
Event: event, | ||
Payload: payload, | ||
} | ||
} | ||
|
||
func (this *Event) CopyID(copyFrom *Event) { | ||
this.ID = copyFrom.ID | ||
} | ||
|
||
func (this *Event) PayloadAsMap() M { | ||
return this.Payload.(M) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package lxtypes | ||
|
||
type ( | ||
KV struct { | ||
Key string `json:"key"` | ||
Value interface{} `json:"value"` | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package lxtypes | ||
|
||
type ( | ||
M = map[string]interface{} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package lxtypes | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/rs/xid" | ||
) | ||
|
||
type ( | ||
Message struct { | ||
User User `json:"user"` | ||
Room Room `json:"room"` | ||
Contents []Content `json:"messages"` | ||
Mode string `json:"mode"` | ||
Raw interface{} `json:"raw"` | ||
} | ||
User struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
Room struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
} | ||
Content struct { | ||
ID string `json:"id"` | ||
Text string `json:"text"` | ||
Attachments []Attachment `json:"attachments"` | ||
} | ||
Attachment struct { | ||
Url string `json:"url"` | ||
Description string `json:"description"` | ||
} | ||
) | ||
|
||
func NewLXMessage(msg M) (*Message, error) { | ||
t, err := json.Marshal(msg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m := new(Message) | ||
if err := json.Unmarshal(t, m); err != nil { | ||
return nil, err | ||
} | ||
return &Message{ | ||
User: m.User, | ||
Room: m.Room, | ||
Contents: m.Contents, | ||
Mode: "", | ||
Raw: m.Raw, | ||
}, nil | ||
} | ||
|
||
func (this *Message) Copy() (*Message, error) { | ||
m, err := this.ToMap() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewLXMessage(m) | ||
} | ||
|
||
func (this *Message) ResetContents() *Message { | ||
this.Contents = make([]Content, 0) | ||
return this | ||
} | ||
|
||
func (this *Message) AddContent(text string, attachments ...Attachment) *Message { | ||
var a []Attachment | ||
if attachments == nil { | ||
a = make([]Attachment, 0) | ||
} else { | ||
a = attachments | ||
} | ||
|
||
this.Contents = append(this.Contents, Content{ | ||
ID: xid.New().String(), | ||
Text: text, | ||
Attachments: a, | ||
}) | ||
return this | ||
} | ||
|
||
func (this *Message) Send() *Message { | ||
this.Mode = "send" | ||
return this | ||
} | ||
|
||
func (this *Message) Reply() *Message { | ||
this.Mode = "reply" | ||
return this | ||
} | ||
|
||
func (this *Message) ToMap() (M, error) { | ||
t, err := json.Marshal(this) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var r M | ||
if err := json.Unmarshal(t, &r); err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} |
Oops, something went wrong.