diff --git a/serialization.go b/serialization.go index 2f54aa9..a1d0518 100644 --- a/serialization.go +++ b/serialization.go @@ -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 { diff --git a/transport_facebook.go b/transport_facebook.go index 7d95a68..c855591 100644 --- a/transport_facebook.go +++ b/transport_facebook.go @@ -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 { @@ -16,7 +17,7 @@ type FacebookTransport struct { Friend string Browser *browser.Browser Serializer DefaultSerializer - ChatUrl string + ChatURL string } func (t *FacebookTransport) DoLogin() bool { @@ -41,8 +42,8 @@ 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) @@ -50,7 +51,7 @@ func (t *FacebookTransport) DoLogin() bool { t.Browser.Click("a[href*=\"/messages/thread/\"]") - t.ChatUrl = t.Browser.Url().String() + t.ChatURL = t.Browser.Url().String() return true @@ -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) } @@ -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{} @@ -103,5 +104,4 @@ func (t *FacebookTransport) Listen() { fmt.Println("Polling...") for { } - return } diff --git a/transport_whatsapp.go b/transport_whatsapp.go index 6b97c1f..a4f066a 100644 --- a/transport_whatsapp.go +++ b/transport_whatsapp.go @@ -12,7 +12,7 @@ import ( // "errors" ) -const YowsupHttpWrapperPath = "../yowsup-http-wrapper/run.py" +const YowsupHTTPWrapperPath = "../yowsup-http-wrapper/run.py" var ResponseChannel chan Response @@ -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 @@ -41,9 +41,9 @@ 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 { @@ -51,23 +51,23 @@ func (t *WhatsappTransport) DaemonizeWrapper() { } } -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) @@ -80,41 +80,39 @@ 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 } @@ -122,7 +120,7 @@ func (t *WhatsappTransport) SendMessage(body string) { 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{} @@ -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)) @@ -195,5 +193,4 @@ func (t *WhatsappTransport) Listen(Callback WhatsappMessageCallback) { } time.Sleep(1 * time.Second) } - return }