Skip to content

Commit

Permalink
Added SendPhoto Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
h4ckitt committed Aug 12, 2021
1 parent 12799df commit 10b1018
Showing 1 changed file with 88 additions and 1 deletion.
89 changes: 88 additions & 1 deletion bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ func (b *Bot) SendMessage(s string, c Chat) (Message, error) {

jsonBody, err := json.Marshal(reply)

if err != nil {
if err != nil { // _, err := bot.SendMessage("Hello "+update.Message.From.Firstname, update.Message.Chat)
//
// if err != nil {
// log.Println(err)
// }
log.Println("Couldn't Marshal Response")
return Message{}, err

Expand All @@ -245,6 +249,10 @@ func (b *Bot) SendMessage(s string, c Chat) (Message, error) {

err = json.NewDecoder(resp.Body).Decode(&response)

if err != nil {
return Message{}, err
}

newMessage = Message{
MessageID: response.Result.MessageId,
Chat: response.Result.Chat,
Expand Down Expand Up @@ -518,3 +526,82 @@ func (b *Bot) SendVideo(file string, caption string, c Chat) error {
}
return nil
}

func (b *Bot) SendPhoto(file string, caption string, c Chat) error {

link := b.APIURL + "/sendPhoto"

if regexp.MustCompile("^(https?)").MatchString(file) {
jsonBody, err := json.Marshal(videoBody{
ChatID: strconv.Itoa(c.ID),
VideoLink: file,
Caption: caption,
})

if err != nil {
log.Println("There Was An Error Marshalling The Object")
return err
}

resp, err := http.Post(link, "application/json", bytes.NewBuffer(jsonBody))

if err != nil {
log.Println("Photo Couldn't Be Sent Successfully, Please Check Internet Source")
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Println("Photo Not Sent Successfully, Check Error Logs For More Details")
body, _ := ioutil.ReadAll(resp.Body)
return errors.New(string(body))
}

return nil
}

photo, err := os.Open(file)

if err != nil {
log.Println("Couldn't Open Specified File For Reading")
return err
}

defer photo.Close()

body := new(bytes.Buffer)

writer := multipart.NewWriter(body)

part, err := writer.CreateFormFile("photo", filepath.Base(file))

io.Copy(part, photo)

writer.WriteField("chat_id", strconv.Itoa(c.ID))

if caption != "" {
writer.WriteField("caption", caption)
}

writer.Close()

client := &http.Client{}
req, _ := http.NewRequest("POST", link, body)
req.Header.Add("Content-Type", writer.FormDataContentType())
resp, err := client.Do(req)

if err != nil {
log.Println("Couldn't Send Photo, Check Internet Connection")
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Println("Photo Not Sent Successfully, Check Error Logs For Details")
body, _ := ioutil.ReadAll(resp.Body)
return errors.New(string(body))
}
return nil
}

0 comments on commit 10b1018

Please sign in to comment.