github.com/fumiama/NanoBot@v0.0.0-20231122134259-c22d8183efca/openapi_v2_files.go (about)

     1  package nano
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/sirupsen/logrus"
     8  )
     9  
    10  // FileType 媒体类型
    11  type FileType int
    12  
    13  const (
    14  	FileTypeImage = iota + 1 // png/jpg
    15  	FileTypeVideo            // mp4
    16  	FileTypeAudio            // silk
    17  	FileTypeFile             // 暂不开放
    18  )
    19  
    20  func (ft FileType) String() string {
    21  	switch ft {
    22  	case FileTypeImage:
    23  		return "图片"
    24  	case FileTypeVideo:
    25  		return "视频"
    26  	case FileTypeAudio:
    27  		return "语音"
    28  	case FileTypeFile:
    29  		return "文件"
    30  	default:
    31  		return "未知类型" + strconv.Itoa(int(ft))
    32  	}
    33  }
    34  
    35  // FilePost QQ 富媒体消息发送请求参数
    36  //
    37  // https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html
    38  type FilePost struct {
    39  	Type       FileType `json:"file_type"`
    40  	URL        string   `json:"url"`
    41  	IsPositive bool     `json:"srv_send_msg"` // IsPositive
    42  	// file_data		否	【暂未支持】
    43  }
    44  
    45  func (fp *FilePost) String() string {
    46  	sb := strings.Builder{}
    47  	sb.WriteString("[v2.")
    48  	sb.WriteString(fp.Type.String())
    49  	sb.WriteString("]")
    50  	if fp.URL == "" {
    51  		sb.WriteString("无链接")
    52  	} else {
    53  		sb.WriteString("链接: ")
    54  		sb.WriteString(fp.URL)
    55  	}
    56  	return sb.String()
    57  }
    58  
    59  // PostFileToQQUser 发送文件到 QQ 用户的 openid
    60  //
    61  // https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html#%E5%8F%91%E9%80%81%E5%88%B0%E5%8D%95%E8%81%8A
    62  func (bot *Bot) PostFileToQQUser(id string, content *FilePost) (*Message, error) {
    63  	logrus.Infoln(getLogHeader(), "<= [Q]单:", id+",", content)
    64  	return bot.postOpenAPIofMessage("/v2/users/"+id+"/files", "", WriteBodyFromJSON(content))
    65  }
    66  
    67  // PostFileToQQGroup 发送文件到 QQ 群的 openid
    68  //
    69  // https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html#%E5%8F%91%E9%80%81%E5%88%B0%E7%BE%A4%E8%81%8A
    70  func (bot *Bot) PostFileToQQGroup(id string, content *FilePost) (*Message, error) {
    71  	logrus.Infoln(getLogHeader(), "<= [Q]群:", id+",", content)
    72  	return bot.postOpenAPIofMessage("/v2/groups/"+id+"/files", "", WriteBodyFromJSON(content))
    73  }
    74  
    75  // MessageMedia used in MessagePost
    76  type MessageMedia struct {
    77  	FileInfo string `json:"file_info"`
    78  }