Skip to content

Commit

Permalink
Simplify some code, make the linter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasinsaurralde committed Mar 20, 2017
1 parent 8ba518b commit 8255b47
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 46 deletions.
3 changes: 1 addition & 2 deletions serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ func (serializer *DefaultSerializer) Serialize(req interface{}, jsonOutput bool)
if jsonOutput {
output, _ = json.Marshal(r)
return output
} else {
return r
}
return r
}

func (serializer *DefaultSerializer) DeserializeRequest(Input []byte) *http.Request {
Expand Down
18 changes: 9 additions & 9 deletions transport_facebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package transports
import (
"errors"
"fmt"
"github.com/headzoo/surf"
"github.com/headzoo/surf/browser"
"io/ioutil"
"net/http"

"github.com/headzoo/surf"
"github.com/headzoo/surf/browser"
)

type FacebookTransport struct {
Expand All @@ -16,7 +17,7 @@ type FacebookTransport struct {
Friend string
Browser *browser.Browser
Serializer DefaultSerializer
ChatUrl string
ChatURL string
}

func (t *FacebookTransport) DoLogin() bool {
Expand All @@ -41,16 +42,16 @@ func (t *FacebookTransport) DoLogin() bool {

fmt.Println("Logged in as", t.Browser.Title(), "?")

FriendUrl := fmt.Sprintf("https://mobile.facebook.com/%s", t.Friend)
err = t.Browser.Open(FriendUrl)
FriendURL := fmt.Sprintf("https://mobile.facebook.com/%s", t.Friend)
err = t.Browser.Open(FriendURL)

if err != nil {
panic(err)
}

t.Browser.Click("a[href*=\"/messages/thread/\"]")

t.ChatUrl = t.Browser.Url().String()
t.ChatURL = t.Browser.Url().String()

return true

Expand All @@ -64,7 +65,7 @@ func (t *FacebookTransport) Prepare() {
t.Browser = surf.NewBrowser()

if !t.DoLogin() {
err := errors.New("Authentication error!")
err := errors.New("Authentication error")
panic(err)
}

Expand All @@ -73,7 +74,7 @@ func (t *FacebookTransport) Prepare() {

func (t *FacebookTransport) Handler(w http.ResponseWriter, originalRequest *http.Request) {

t.Browser.Open(t.ChatUrl)
t.Browser.Open(t.ChatURL)

client := &http.Client{}

Expand Down Expand Up @@ -103,5 +104,4 @@ func (t *FacebookTransport) Listen() {
fmt.Println("Polling...")
for {
}
return
}
67 changes: 32 additions & 35 deletions transport_whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// "errors"
)

const YowsupHttpWrapperPath = "../yowsup-http-wrapper/run.py"
const YowsupHTTPWrapperPath = "../yowsup-http-wrapper/run.py"

var ResponseChannel chan Response

Expand All @@ -22,8 +22,8 @@ type WhatsappTransport struct {
Password string
Contact string
YowsupWrapperPort string
YowsupWrapperUrl string
UseTor bool
YowsupWrapperURL string
UseTor bool
Serializer DefaultSerializer
Messages []WhatsappMessage
// ResponseChannel chan Response
Expand All @@ -41,33 +41,33 @@ type WhatsappMessageCallback func(*WhatsappTransport)
func (t *WhatsappTransport) DaemonizeWrapper() {
fmt.Println("WhatsappTransport, daemonizing YowsupWrapper...")

t.YowsupWrapperUrl = fmt.Sprintf("http://127.0.0.1:%s/", t.YowsupWrapperPort)
t.YowsupWrapperURL = fmt.Sprintf("http://127.0.0.1:%s/", t.YowsupWrapperPort)

cmd := exec.Command("python3", YowsupHttpWrapperPath, t.Login, t.Password, t.YowsupWrapperPort)
cmd := exec.Command("python3", YowsupHTTPWrapperPath, t.Login, t.Password, t.YowsupWrapperPort)
err := cmd.Run()

if err != nil {
panic(err)
}
}

func (t *WhatsappTransport) GetMessageIds() []string {
MessageIds := make([]string, 0)
for _, Message := range t.Messages {
MessageIds = append(MessageIds, Message.Id)
func (t *WhatsappTransport) GetMessageIDs() []string {
ids := make([]string, 0)
for _, message := range t.Messages {
ids = append(ids, message.Id)
}
return MessageIds
return ids
}

func (t *WhatsappTransport) PurgeMessage(Id string) {
messagesUrl := fmt.Sprintf("%s%s?id=%s", t.YowsupWrapperUrl, "messages", Id)
deleteRequest, _ := http.NewRequest("DELETE", messagesUrl, nil)
messagesURL := fmt.Sprintf("%s%s?id=%s", t.YowsupWrapperURL, "messages", Id)
deleteRequest, _ := http.NewRequest("DELETE", messagesURL, nil)
http.DefaultClient.Do(deleteRequest)
}

func (t *WhatsappTransport) FetchMessages() {
messagesUrl := strings.Join([]string{t.YowsupWrapperUrl, "messages"}, "")
resp, err := http.Get(messagesUrl)
messagesURL := strings.Join([]string{t.YowsupWrapperURL, "messages"}, "")
resp, err := http.Get(messagesURL)

if err != nil {
// fmt.Println( "Wrapper error:", err)
Expand All @@ -80,49 +80,47 @@ func (t *WhatsappTransport) FetchMessages() {

var messageList map[string]interface{}

jsonErr := json.Unmarshal(rawBody, &messageList)
err = json.Unmarshal(rawBody, &messageList)

if jsonErr != nil {
if err != nil {
return
}

MessageIds := t.GetMessageIds()
messageIDs := t.GetMessageIDs()

for Id, Values := range messageList {
ValuesMap := Values.(map[string]interface{})
Message := WhatsappMessage{Id: Id, Body: ValuesMap["body"].(string), Origin: ValuesMap["origin"].(string)}
Exists := false
for id, values := range messageList {
valuesMap := values.(map[string]interface{})
message := WhatsappMessage{Id: id, Body: valuesMap["body"].(string), Origin: valuesMap["origin"].(string)}
exists := false

for _, ExistingId := range MessageIds {
if ExistingId == Id {
Exists = true
for _, existingID := range messageIDs {
if existingID == id {
exists = true
return
}
}

if !Exists {
t.Messages = append(t.Messages, Message)
if !exists {
t.Messages = append(t.Messages, message)
}
}

return
}

func (t *WhatsappTransport) SendMessage(body string) {
messagesUrl := strings.Join([]string{t.YowsupWrapperUrl, "messages"}, "")
messagesURL := strings.Join([]string{t.YowsupWrapperURL, "messages"}, "")
message := WhatsappMessage{Body: body, Dest: t.Contact}
jsonBuffer, _ := json.Marshal(&message)
http.Post(messagesUrl, "application/json", bytes.NewReader(jsonBuffer))
http.Post(messagesURL, "application/json", bytes.NewReader(jsonBuffer))

fmt.Println("<-- Sending message\n", message, "\n")
fmt.Println("<-- Sending message\n", message)

return
}

func (t *WhatsappTransport) Prepare() {
// fmt.Println("WhatsappTransport, Prepare()")

t.YowsupWrapperUrl = fmt.Sprintf("http://127.0.0.1:%s/", t.YowsupWrapperPort)
t.YowsupWrapperURL = fmt.Sprintf("http://127.0.0.1:%s/", t.YowsupWrapperPort)

t.Serializer = DefaultSerializer{}

Expand Down Expand Up @@ -151,8 +149,8 @@ func (t *WhatsappTransport) Handler(w http.ResponseWriter, originalRequest *http
w.Header().Set(HeaderKey, HeaderValue[0])
}

fmt.Println("--> Receiving message\n", response, "\n")
fmt.Println("<-> Forwarding message\n", response, "\n")
fmt.Println("--> Receiving message\n", response)
fmt.Println("<-> Forwarding message\n", response)

w.Write([]byte(response.Body))

Expand Down Expand Up @@ -195,5 +193,4 @@ func (t *WhatsappTransport) Listen(Callback WhatsappMessageCallback) {
}
time.Sleep(1 * time.Second)
}
return
}

0 comments on commit 8255b47

Please sign in to comment.