Skip to content

Commit

Permalink
Some refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
onrik committed Jul 8, 2016
1 parent 9e22f29 commit e8b38fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 121 deletions.
47 changes: 24 additions & 23 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package micha
import (
"encoding/json"
"fmt"
"github.com/onrik/micha/http"
"io"
"log"
"net/url"
Expand All @@ -14,7 +15,7 @@ const (
FILE_API_URL = "https://api.telegram.org/file/bot%s/%s"
)

type ApiResponse struct {
type Response struct {
Ok bool `json:"ok"`
ErrorCode int `json:"error_code"`
Description string `json:"description"`
Expand Down Expand Up @@ -51,21 +52,21 @@ func (bot *Bot) buildUrl(method string) string {

// Decode response result to target object
func (bot *Bot) decodeResponse(data []byte, target interface{}) error {
apiResponse := new(ApiResponse)
if err := json.Unmarshal(data, apiResponse); err != nil {
response := new(Response)
if err := json.Unmarshal(data, response); err != nil {
return fmt.Errorf("Decode response error (%s)", err.Error())
}

if !apiResponse.Ok {
return fmt.Errorf("Response status: %d (%s)", apiResponse.ErrorCode, apiResponse.Description)
if !response.Ok {
return fmt.Errorf("Response status: %d (%s)", response.ErrorCode, response.Description)
}

if target == nil {
// Don't need to decode result
return nil
}

if err := json.Unmarshal(apiResponse.Result, target); err != nil {
if err := json.Unmarshal(response.Result, target); err != nil {
return fmt.Errorf("Decode result error (%s)", err.Error())
} else {
return nil
Expand All @@ -74,7 +75,7 @@ func (bot *Bot) decodeResponse(data []byte, target interface{}) error {

// Make GET request to Telegram API
func (bot *Bot) get(method string, params url.Values, target interface{}) error {
response, err := get(bot.buildUrl(method) + "?" + params.Encode())
response, err := http.Get(bot.buildUrl(method) + "?" + params.Encode())
if err != nil {
return err
} else {
Expand All @@ -84,7 +85,7 @@ func (bot *Bot) get(method string, params url.Values, target interface{}) error

// Make POST request to Telegram API
func (bot *Bot) post(method string, data, target interface{}) error {
response, err := post(bot.buildUrl(method), data)
response, err := http.Post(bot.buildUrl(method), data)
if err != nil {
return err
} else {
Expand All @@ -93,8 +94,8 @@ func (bot *Bot) post(method string, data, target interface{}) error {
}

// Make POST request to Telegram API
func (bot *Bot) postMultipart(method string, file *FileToSend, params url.Values, target interface{}) error {
response, err := postMultipart(bot.buildUrl(method), file, params)
func (bot *Bot) postMultipart(method string, file *http.File, params url.Values, target interface{}) error {
response, err := http.PostMultipart(bot.buildUrl(method), file, params)
if err != nil {
return err
} else {
Expand Down Expand Up @@ -178,14 +179,14 @@ func (bot *Bot) SendPhotoFile(chatId int64, file io.ReadCloser, options *SendPho
return nil, err
}

fileToSend := &FileToSend{
f := &http.File{
File: file,
Fieldname: "photo",
Filename: "photo.png",
}

message := new(Message)
err = bot.postMultipart("sendPhoto", fileToSend, values, message)
err = bot.postMultipart("sendPhoto", f, values, message)

return message, err
}
Expand All @@ -208,14 +209,14 @@ func (bot *Bot) SendAudioFile(chatId int64, file io.ReadCloser, options *SendAud
return nil, err
}

fileToSend := &FileToSend{
f := &http.File{
File: file,
Fieldname: "audio",
Filename: "audio.mp3",
}

message := new(Message)
err = bot.postMultipart("sendAudio", fileToSend, values, message)
err = bot.postMultipart("sendAudio", f, values, message)

return message, err
}
Expand All @@ -238,14 +239,14 @@ func (bot *Bot) SendDocumentFile(chatId int64, documentName string, file io.Read
return nil, err
}

fileToSend := &FileToSend{
f := &http.File{
File: file,
Fieldname: "document",
Filename: documentName,
}

message := new(Message)
err = bot.postMultipart("sendDocument", fileToSend, values, message)
err = bot.postMultipart("sendDocument", f, values, message)

return message, err
}
Expand All @@ -268,14 +269,14 @@ func (bot *Bot) SendStickerFile(chatId int64, file io.ReadCloser, options *SendS
return nil, err
}

fileToSend := &FileToSend{
f := &http.File{
File: file,
Fieldname: "sticker",
Filename: "sticker.webp",
}

message := new(Message)
err = bot.postMultipart("sendSticker", fileToSend, values, message)
err = bot.postMultipart("sendSticker", f, values, message)

return message, err
}
Expand All @@ -298,14 +299,14 @@ func (bot *Bot) SendVideoFile(chatId int64, file io.ReadCloser, options *SendVid
return nil, err
}

fileToSend := &FileToSend{
f := &http.File{
File: file,
Fieldname: "video",
Filename: "video.mp4",
}

message := new(Message)
err = bot.postMultipart("sendVideo", fileToSend, values, message)
err = bot.postMultipart("sendVideo", f, values, message)

return message, err
}
Expand All @@ -330,14 +331,14 @@ func (bot *Bot) SendVoiceFile(chatId int64, file io.ReadCloser, options *SendVoi
return nil, err
}

fileToSend := &FileToSend{
f := &http.File{
File: file,
Fieldname: "voice",
Filename: "voice.ogg",
}

message := new(Message)
err = bot.postMultipart("sendVoice", fileToSend, values, message)
err = bot.postMultipart("sendVoice", f, values, message)

return message, err
}
Expand Down Expand Up @@ -419,7 +420,7 @@ func (bot *Bot) GetUserProfilePhotos(userID int64, options *GetUserProfilePhotos
// It is guaranteed that the link will be valid for at least 1 hour.
// When the link expires, a new one can be requested by calling getFile again.
func (bot *Bot) GetFile(fileID string) (*File, error) {
response, err := get(fmt.Sprintf(FILE_API_URL, bot.token, fileID))
response, err := http.Get(fmt.Sprintf(FILE_API_URL, bot.token, fileID))
if err != nil {
return nil, err
}
Expand Down
98 changes: 0 additions & 98 deletions http.go

This file was deleted.

0 comments on commit e8b38fa

Please sign in to comment.