func sendReplyToTelegramWithPh(chatID int64, message string, photoURL string) { token :=…
- User
func sendReplyToTelegramWithPh(chatID int64, message string, photoURL string) { token := os.Getenv("TELEGRAM_TOKEN") apiUrl := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", token)
reqData := struct { ChatID int64
json:"chat_id"Text stringjson:"text"Photo []stringjson:"photo"// Изменено: добавлено поле photo }{ ChatID: chatID, Text: message, Photo: []string{photoURL}, // Добавлено: URL картинки в массив }body, err := json.Marshal(reqData) if err != nil { fmt.Printf("Error marshaling request data: %v\n", err) return }
resp, err := http.Post(apiUrl, "application/json", bytes.NewBuffer(body)) if err != nil { fmt.Printf("Error sending message: %v\n", err) return } defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { bodyResp, _ := io.ReadAll(resp.Body) fmt.Printf("Failed to send message: %s, response: %s\n", resp.Status, string(bodyResp)) } }
message := fmt.Sprintf("Новая серия!\n%s\nСезон:%d Эпизод:%d", data.Name, data.LastEpisodeToAir.SeasonNumber, data.LastEpisodeToAir.EpisodeNumber) photoURL := fmt.Sprintf("https://imagetmdb.cub.red/t/p/w300%s", data.LastEpisodeToAir.StillPath) sendReplyToTelegramWithPh(telegramID, message, photoURL)
почему не отправляются фото?
- Assistant
Ваша функция
sendReplyToTelegramWithPhпытается отправить сообщение, используя метод Telegram APIsendMessage, который предназначен только для отправки текстовых сообщений. Чтобы отправить фотографию через Telegram, вам нужно использовать методsendPhoto, а неsendMessage. Вот как можно модифицировать вашу функцию:go