WeChat public platform - advanced group sending interface
When the WeChat public account uses the interface, operations such as acquisition and invocation of multimedia files and multimedia messages are performed through media_id. Through this interface, WeChat public accounts can upload and download multimedia files.
1. Upload multimedia files (here takes uploading pictures as an example)
uses IdMultipartFormData;
const
UpMediaUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s';
function UpMedia(HTTP: TIdHTTP; AccessToken, MediaType, MediaFile: String): String; where J: TJSONObject; Url: String; temp: String; FormData: TIdMultiPartFormDataStream; RespData: TStringStream; begin RespData := TStringStream.Create(''); FormData := TIdMultiPartFormDataStream.Create; J := TJSONObject.Create; try FormData.AddFile('media', MediaFile); Url := Format(UpMediaUrl, [AccessToken, MediaType]); HTTP.Post(Url, FormData, RespData); temp := RespData.DataString; HTTP.Request.Referer := Url; J := TJSONObject.ParseJSONValue(temp) as TJSONObject; if J.Count > 0 then Result := J.GetValue('media_id').Value else Result := ''; finally FormData.Free; RespData.Free; J.Free; end; end; //Return the media_id of the media file for backup
2. Upload graphic message material
TGRPNews = record MediaID:String;//media_id of thumbnail Author:String;//Author Title:String;//Title SorceUrl:String;//Read the address of the original text Content:String;//The content of the graphic message supports html tags Digest:String;//Summary ShowCover:String;//Whether to show the cover end;
const
UpNewsUrl ='https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=%s';
function UpNews(Num: Integer; AccessToken: String): String;//Num max is 10 where J: TJSONObject; N: array of TJSONObject; Url: String; temp: String; C: array of TStringList; G: array of TGRPNews; i: Integer; T: TStringList; begin J := TJSONObject.Create; T := TStringList.Create; T.LoadFromFile('F:\t.txt');//The title of the picture message needs to be written in advance SetLength(N, Num); SetLength(C, Num);//The content of the graphic message SetLength(G, Num);//Custom text message structure try J.AddPair('articles', TJSONArray.Create); with J.GetValue('articles') as TJSONArray do for i := 0 to Num - 1 do begin C[i] := TStringList.Create; C[i].LoadFromFile(Format('F:\%d.txt', [i]));//Call the content of the graphic message, which needs to be written in advance G[i].MediaID := UpMedia(AccessToken, 'image', Format('F:\%d.jpg', [i])); G[i].Author := ''; G[i].Title := T[i]; G[i].SorceUrl := ''; G[i].Content := C[i].Text; G[i].Digest := T[i]; G[i].ShowCover := '0'; try N[i] := TJSONObject.Create; N[i].AddPair('thumb_media_id', G[i].MediaID); N[i].AddPair('author', G[i].Author); N[i].AddPair('title', G[i].Title); N[i].AddPair('content_source_url', G[i].SorceUrl); N[i].AddPair('content', G[i].Content); N[i].AddPair('digest', G[i].Digest); N[i].AddPair('show_cover_pic', G[i].ShowCover); Add(N[i]); finally C[i].Free; end; end; Url := Format(UpNewsUrl, [AccessToken]); temp := PostMethod(Url, UTF8Encode(J.ToString), 1); J := TJSONObject.ParseJSONValue(temp) as TJSONObject; if J.Count > 0 then Result := J.GetValue('media_id').Value; finally J.Free; T.Free; end; end; //Return the media_id of the graphic message for backup
3. Preview the uploaded graphic message
const PreviewUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=%s'; function GroupPreviewNews(OpenID, MediaID, AccessToken: String):TJSONObject; where J: TJSONObject; Url: String; temp: String; begin J := TJSONObject.Create; try J.AddPair('touser', OpenID); J.AddPair('mpnews', TJSONObject.Create); with J.GetValue('mpnews') as TJSONObject do begin AddPair('media_id', MediaID); end; J.AddPair('msgtype', 'mpnews'); Url := Format(PreviewUrl, [AccessToken]); temp:=PostMethod(Url, UTF8Encode(J.ToString), 1); Result:=TJSONObject.ParseJSONValue(temp) as TJSONObject; finally J.Free; end; end;
4. Send pictures and text messages by group
const GroupSendUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=%s'; function GroupSendNews(GroupID, MediaID, AccessToken: String):TJSONObject; where J: TJSONObject; Url: String; temp: String; begin J := TJSONObject.Create; try J.AddPair('filter', TJSONObject.Create); with J.GetValue('filter') as TJSONObject do begin AddPair('is_to_all', TJSONFalse.Create); AddPair('group_id', GroupID); end; J.AddPair('mpnews', TJSONObject.Create); with J.GetValue('mpnews') as TJSONObject do begin AddPair('media_id', MediaID); end; J.AddPair('msgtype', 'mpnews'); Url := Format(GroupSendUrl, [AccessToken]); temp:=PostMethod(Url, UTF8Encode(J.ToString), 1); Result:=TJSONObject.ParseJSONValue(temp) as TJSONObject; finally J.Free; end; end;