Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

57 lines
1.2KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. //MediaID : json response for uploading media
  7. type MediaID struct {
  8. Type string `json:"type"`
  9. MediaID string `json:"media_id"`
  10. CreateAt int64 `json:"created_at"`
  11. }
  12. func uploadImage(filename string) (mediaid string) {
  13. url, _ := getPostImageURL()
  14. jstr, _ := postFileForm(filename, url, "media")
  15. //{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
  16. var m = MediaID{}
  17. json.Unmarshal([]byte(jstr), &m)
  18. return
  19. }
  20. func checkImageSanity() bool {
  21. //check file size should < 2M
  22. fmt.Println(" should check image file size")
  23. return true
  24. }
  25. func getPostImageURL() (url string, err error) {
  26. return getMediaPostURL("image")
  27. }
  28. func getPostVoiceURL() (url string, err error) {
  29. return getMediaPostURL("voice")
  30. }
  31. func getPostVideoURL() (url string, err error) {
  32. return getMediaPostURL("video")
  33. }
  34. func getPostThumbURL() (url string, err error) {
  35. return getMediaPostURL("thumb")
  36. }
  37. func getMediaPostURL(t string) (url string, err error) {
  38. atk, err := GetAccessToken()
  39. if err != nil {
  40. url = ""
  41. } else {
  42. url = fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s", atk, t)
  43. }
  44. return
  45. }