changeset 14:7c17fbb05306

Move Pull code into own file
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 09 Dec 2016 21:49:06 +0100
parents 992cc8d12603
children 8ca52684ea6d
files http.go pull.go webhook.go
diffstat 3 files changed, 92 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- a/http.go	Fri Dec 09 21:45:49 2016 +0100
+++ b/http.go	Fri Dec 09 21:49:06 2016 +0100
@@ -1,8 +1,6 @@
 package main
 
 import (
-	"bytes"
-	"encoding/json"
 	"fmt"
 	"io/ioutil"
 	"log"
@@ -42,89 +40,6 @@
 	return fmt.Sprintf("https://api.telegram.org/bot%s/%s", *flagToken, request)
 }
 
-// After the webhook is called, it should process the request, respond with http.StatusOK (if appropriate),
-// and then call the sendMessage method.
-func pullUpdates(off uint64) (uint64, error) {
-	gU := getUpdates{Offset: off, Timeout: uint64(*flagDeadline)}
-	body, err := json.Marshal(gU)
-
-	if err != nil {
-		log.Println(err)
-		return off, err
-	}
-
-	rp, err := pullClient.Post(buildURL(getUpdatesMethod), jsonBodyType, bytes.NewBuffer(body))
-	defer rp.Body.Close()
-
-	if err != nil {
-		log.Println("Couldn't read updates body:", err)
-		return off, err
-	}
-
-	rbody, err := ioutil.ReadAll(rp.Body)
-
-	log.Println("Received update:", string(rbody))
-
-	upds := updateReply{}
-	err = json.Unmarshal(rbody, &upds)
-
-	if err != nil {
-		log.Println("Couldn't unmarshal update:", err)
-		return off, err
-	}
-
-	if !upds.OK {
-		log.Println("Non-OK update response:", upds)
-		return off, err
-	}
-
-	for u := range upds.Result {
-		srvStatus.numCommands++
-
-		sM, err := dispatch(upds.Result[u].Message)
-
-		if err != nil {
-			log.Println(err)
-			srvStatus.errors++
-		}
-
-		err = sendReply(sM)
-
-		if err != nil {
-			srvStatus.apiErrors++
-		}
-	}
-
-	if len(upds.Result) > 0 {
-		return upds.Result[len(upds.Result)-1].Update_ID + 1, nil
-	}
-	return off, nil
-}
-
-func sendReply(msg sendMessage) error {
-	body, err := json.Marshal(msg)
-
-	if err != nil {
-		log.Println(err)
-		return err
-	}
-
-	log.Println("Msg response:", string(body))
-
-	url := buildURL(sendMessageMethod)
-
-	rp, err := defaultClient.Post(url, jsonBodyType, bytes.NewBuffer(body))
-
-	if err != nil {
-		log.Println(err)
-		return err
-	} else if rp.StatusCode != http.StatusOK {
-		log.Println("Error:", rp.StatusCode, rp.Status)
-	}
-
-	return nil
-}
-
 // Handles simple text posted to a debug handler (/debug).
 func debugHandler(rp http.ResponseWriter, rq *http.Request) {
 	body, err := ioutil.ReadAll(rq.Body)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pull.go	Fri Dec 09 21:49:06 2016 +0100
@@ -0,0 +1,92 @@
+package main
+
+import (
+	"bytes"
+	"encoding/json"
+	"io/ioutil"
+	"log"
+	"net/http"
+)
+
+// After the webhook is called, it should process the request, respond with http.StatusOK (if appropriate),
+// and then call the sendMessage method.
+func pullUpdates(off uint64) (uint64, error) {
+	gU := getUpdates{Offset: off, Timeout: uint64(*flagDeadline)}
+	body, err := json.Marshal(gU)
+
+	if err != nil {
+		log.Println(err)
+		return off, err
+	}
+
+	rp, err := pullClient.Post(buildURL(getUpdatesMethod), jsonBodyType, bytes.NewBuffer(body))
+	defer rp.Body.Close()
+
+	if err != nil {
+		log.Println("Couldn't read updates body:", err)
+		return off, err
+	}
+
+	rbody, err := ioutil.ReadAll(rp.Body)
+
+	log.Println("Received update:", string(rbody))
+
+	upds := updateReply{}
+	err = json.Unmarshal(rbody, &upds)
+
+	if err != nil {
+		log.Println("Couldn't unmarshal update:", err)
+		return off, err
+	}
+
+	if !upds.OK {
+		log.Println("Non-OK update response:", upds)
+		return off, err
+	}
+
+	for u := range upds.Result {
+		srvStatus.numCommands++
+
+		sM, err := dispatch(upds.Result[u].Message)
+
+		if err != nil {
+			log.Println(err)
+			srvStatus.errors++
+		}
+
+		err = sendReply(sM)
+
+		if err != nil {
+			srvStatus.apiErrors++
+		}
+	}
+
+	if len(upds.Result) > 0 {
+		return upds.Result[len(upds.Result)-1].Update_ID + 1, nil
+	}
+	return off, nil
+}
+
+func sendReply(msg sendMessage) error {
+	body, err := json.Marshal(msg)
+
+	if err != nil {
+		log.Println(err)
+		return err
+	}
+
+	log.Println("Msg response:", string(body))
+
+	url := buildURL(sendMessageMethod)
+
+	rp, err := defaultClient.Post(url, jsonBodyType, bytes.NewBuffer(body))
+
+	if err != nil {
+		log.Println(err)
+		return err
+	} else if rp.StatusCode != http.StatusOK {
+		log.Println("Error:", rp.StatusCode, rp.Status)
+	}
+
+	return nil
+}
--- a/webhook.go	Fri Dec 09 21:45:49 2016 +0100
+++ b/webhook.go	Fri Dec 09 21:49:06 2016 +0100
@@ -109,8 +109,6 @@
 		return
 	}
 
-	log.Println("Received update", upd.Update_ID)
-
 	responseMsg, err := dispatch(upd.Message)
 
 	if err != nil {