Usinc fayec client instead of wray
This commit is contained in:
@ -5,8 +5,6 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/karmanyaahm/wray"
|
||||
|
||||
"github.com/beeper/groupme-lib"
|
||||
)
|
||||
|
||||
@ -15,73 +13,29 @@ import (
|
||||
|
||||
var authorizationToken = "ABCD"
|
||||
|
||||
// This adapts your faye library to an interface compatible with this library
|
||||
type FayeClient struct {
|
||||
*wray.FayeClient
|
||||
}
|
||||
|
||||
func (fc FayeClient) WaitSubscribe(channel string, msgChannel chan groupme.PushMessage) {
|
||||
c_new := make(chan wray.Message)
|
||||
fc.FayeClient.WaitSubscribe(channel, c_new)
|
||||
//converting between types because channels don't support interfaces well
|
||||
go func() {
|
||||
for i := range c_new {
|
||||
msgChannel <- i
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// for authentication, specific implementation will vary based on faye library
|
||||
type AuthExt struct{}
|
||||
|
||||
func (a *AuthExt) In(wray.Message) {}
|
||||
func (a *AuthExt) Out(m wray.Message) {
|
||||
groupme.OutMsgProc(m)
|
||||
}
|
||||
|
||||
// specific to faye library
|
||||
type fayeLogger struct{}
|
||||
|
||||
func (l fayeLogger) Infof(f string, a ...interface{}) {
|
||||
log.Printf("[INFO] : "+f, a...)
|
||||
}
|
||||
func (l fayeLogger) Errorf(f string, a ...interface{}) {
|
||||
log.Printf("[ERROR] : "+f, a...)
|
||||
}
|
||||
func (l fayeLogger) Debugf(f string, a ...interface{}) {
|
||||
log.Printf("[DEBUG] : "+f, a...)
|
||||
}
|
||||
func (l fayeLogger) Warnf(f string, a ...interface{}) {
|
||||
log.Printf("[WARN] : "+f, a...)
|
||||
}
|
||||
|
||||
// A short program that subscribes to 2 groups and 2 direct chats
|
||||
// and prints out all recognized events in those
|
||||
func main() {
|
||||
|
||||
//Create and initialize fayeclient
|
||||
fc := FayeClient{wray.NewFayeClient(groupme.PushServer)}
|
||||
fc.SetLogger(fayeLogger{})
|
||||
fc.AddExtension(&AuthExt{})
|
||||
//for additional logging uncomment the following line
|
||||
//fc.AddExtension(fc.FayeClient)
|
||||
|
||||
//create push subscription and start listening
|
||||
p := groupme.NewPushSubscription(context.Background())
|
||||
go p.StartListening(context.TODO(), fc)
|
||||
err := p.Connect(context.TODO(), authorizationToken)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Create a new client with your auth token
|
||||
client := groupme.NewClient(authorizationToken)
|
||||
User, _ := client.MyUser(context.Background())
|
||||
//Subscribe to get messages and events for the specific user
|
||||
err := p.SubscribeToUser(context.Background(), User.ID, authorizationToken)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
//handles (in this case prints) all messages
|
||||
p.AddFullHandler(Handler{User: User})
|
||||
|
||||
//Subscribe to get messages and events for the specific user
|
||||
err = p.SubscribeToUser(context.Background(), User.ID)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get the groups your user is part of
|
||||
groups, err := client.IndexGroups(
|
||||
context.Background(),
|
||||
@ -97,7 +51,7 @@ func main() {
|
||||
}
|
||||
//Subscribe to those groups
|
||||
for _, j := range groups {
|
||||
err = p.SubscribeToGroup(context.TODO(), j.ID, authorizationToken)
|
||||
err = p.SubscribeToGroup(context.TODO(), j.ID)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -111,7 +65,7 @@ func main() {
|
||||
})
|
||||
//subscribe to all those chats
|
||||
for _, j := range chats {
|
||||
err = p.SubscribeToDM(context.TODO(), j.LastMessage.ConversationID, authorizationToken)
|
||||
err = p.SubscribeToDM(context.TODO(), j.LastMessage.ConversationID)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user