Exemplo n.º 1
0
func replyCard(messageID int, chatID int, card Card, c appengine.Context, isgold bool) error {
	ms := multipartstreamer.New()
	client := urlfetch.Client(c)
	var err error
	//Writing other params
	params := make(map[string]string)
	params["chat_id"] = strconv.Itoa(chatID)
	params["reply_to_message_id"] = strconv.Itoa(messageID)
	params["caption"] = card.Name
	ms.WriteFields(params)

	//Getting the image
	var resp *http.Response
	var extension string
	var paramType string
	var methodName string
	if isgold {
		resp, err = client.Get(card.ImgGold)
		c.Debugf("downloading image status: %v", resp.Status)
		extension = "gif"
		paramType = "document"
		methodName = "sendDocument"
	} else {
		resp, err = client.Get(card.Img)
		c.Debugf("downloading image status: %v", resp.Status)
		extension = "png"
		paramType = "photo"
		methodName = "sendPhoto"
	}

	defer resp.Body.Close()

	contentLength, _ := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
	c.Debugf("%v", contentLength)
	ms.WriteReader(paramType, "card."+extension, contentLength, resp.Body)
	req, _ := http.NewRequest("POST", BASE_URL+methodName, nil)
	ms.SetupRequest(req)

	resp, err = client.Do(req)
	//getting the response
	decoder := json.NewDecoder(resp.Body)
	response := botApi.APIResponse{}
	err = decoder.Decode(&response)

	if response.Ok {
		var message botApi.Message
		json.Unmarshal(response.Result, &message)
		insertSentCard(c, card, message.Photo[0].FileID, isgold)
		c.Infof("sent card image: %v", card.Name)
	} else {
		c.Errorf("ok: %v, error_code : %v, description: %v",
			response.Ok, response.ErrorCode, response.Description)
	}
	return err
}
Exemplo n.º 2
0
func main() {
	defaultPath, _ := os.Getwd()
	defaultFile := filepath.Join(defaultPath, "streamer.go")
	fullpath := flag.String("path", defaultFile, "Path to the include in the multipart data.")
	flag.Parse()

	ms := multipartstreamer.New()

	fmt.Println("Adding the file to the multipart writer")
	ms.WriteFile("file", *fullpath)
	reader := ms.GetReader()

	fmt.Println("Writing the multipart data to a file")
	file, _ := os.Create("streamtest")
	io.Copy(file, reader)
}
Exemplo n.º 3
0
// UploadFile makes a request to the API with a file.
//
// Requires the parameter to hold the file not be in the params.
// File should be a string to a file path, a FileBytes struct, or a FileReader struct.
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) {
	ms := multipartstreamer.New()
	ms.WriteFields(params)

	switch f := file.(type) {
	case string:
		fileHandle, err := os.Open(f)
		if err != nil {
			return APIResponse{}, err
		}
		defer fileHandle.Close()

		fi, err := os.Stat(f)
		if err != nil {
			return APIResponse{}, err
		}

		ms.WriteReader(fieldname, fileHandle.Name(), fi.Size(), fileHandle)
	case FileBytes:
		buf := bytes.NewBuffer(f.Bytes)
		ms.WriteReader(fieldname, f.Name, int64(len(f.Bytes)), buf)
	case FileReader:
		if f.Size == -1 {
			data, err := ioutil.ReadAll(f.Reader)
			if err != nil {
				return APIResponse{}, err
			}
			buf := bytes.NewBuffer(data)

			ms.WriteReader(fieldname, f.Name, int64(len(data)), buf)

			break
		}

		ms.WriteReader(fieldname, f.Name, f.Size, f.Reader)
	default:
		return APIResponse{}, errors.New("bad file type")
	}

	req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), nil)
	ms.SetupRequest(req)
	if err != nil {
		return APIResponse{}, err
	}

	res, err := bot.Client.Do(req)
	if err != nil {
		return APIResponse{}, err
	}
	defer res.Body.Close()

	bytes, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return APIResponse{}, err
	}

	if bot.Debug {
		log.Println(string(bytes[:]))
	}

	var apiResp APIResponse
	json.Unmarshal(bytes, &apiResp)

	return apiResp, nil
}
Exemplo n.º 4
0
package http

import (
	"crypto/tls"
	"io"
	"net/http"

	"github.com/technoweenie/multipartstreamer"
)

var LargeMultiPartUpload = func(conn ConnAuth, paramName, filename string, fileSize int64, fileRef io.Reader, params map[string]string) (res *http.Response, err error) {
	var req *http.Request
	ms := multipartstreamer.New()
	ms.WriteReader(paramName, filename, fileSize, fileRef)
	if req, err = http.NewRequest("POST", conn.Url, nil); err == nil {
		if conn.Username != "" && conn.Password != "" {
			req.SetBasicAuth(conn.Username, conn.Password)
		}
		ms.SetupRequest(req)
		tr := &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		}
		client := &http.Client{Transport: tr}
		res, err = client.Do(req)
	}
	return
}
Exemplo n.º 5
0
// UploadFile makes a request to the API with a file.
//
// Requires the parameter to hold the file not be in the params.
// File should be a string to a file path, a FileBytes struct,
// a FileReader struct, or a url.URL.
//
// Note that if your FileReader has a size set to -1, it will read
// the file into memory to calculate a size.
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) {
	ms := multipartstreamer.New()

	switch f := file.(type) {
	case string:
		ms.WriteFields(params)

		fileHandle, err := os.Open(f)
		if err != nil {
			return APIResponse{}, err
		}
		defer fileHandle.Close()

		fi, err := os.Stat(f)
		if err != nil {
			return APIResponse{}, err
		}

		ms.WriteReader(fieldname, fileHandle.Name(), fi.Size(), fileHandle)
	case FileBytes:
		ms.WriteFields(params)

		buf := bytes.NewBuffer(f.Bytes)
		ms.WriteReader(fieldname, f.Name, int64(len(f.Bytes)), buf)
	case FileReader:
		ms.WriteFields(params)

		if f.Size != -1 {
			ms.WriteReader(fieldname, f.Name, f.Size, f.Reader)

			break
		}

		data, err := ioutil.ReadAll(f.Reader)
		if err != nil {
			return APIResponse{}, err
		}

		buf := bytes.NewBuffer(data)

		ms.WriteReader(fieldname, f.Name, int64(len(data)), buf)
	case url.URL:
		params[fieldname] = f.String()

		ms.WriteFields(params)
	default:
		return APIResponse{}, errors.New(ErrBadFileType)
	}

	method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint)

	req, err := http.NewRequest("POST", method, nil)
	if err != nil {
		return APIResponse{}, err
	}

	ms.SetupRequest(req)

	res, err := bot.Client.Do(req)
	if err != nil {
		return APIResponse{}, err
	}
	defer res.Body.Close()

	bytes, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return APIResponse{}, err
	}

	if bot.Debug {
		log.Println(string(bytes))
	}

	var apiResp APIResponse
	json.Unmarshal(bytes, &apiResp)

	if !apiResp.Ok {
		return APIResponse{}, errors.New(apiResp.Description)
	}

	return apiResp, nil
}