Delphi XE7 用indy開發微信公眾平台(9)- 高級群發接口


高級群發接口

原文鏈接:http://www.cnblogs.com/devinlee/p/4282748.html

掃下方二維碼關注,測試效果

1、上傳多媒體文件(這里以上傳圖片為例)

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;
var
  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;

//返回媒體文件的media_id備用

2、上傳圖文消息素材

  TGRPNews = record
    MediaID:String;//縮略圖的media_id
    Author:String;//作者
    Title:String;//標題
    SorceUrl:String;//閱讀原文的地址
    Content:String;//圖文消息的內容支持html標簽
    Digest:String;//摘要
    ShowCover:String;//是否顯示封面
  end;
const

  UpNewsUrl ='https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=%s';

function UpNews(Num: Integer; AccessToken: String): String;//Num最大為10
var
  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');//圖消息的title,需提前寫好
  SetLength(N, Num);
  SetLength(C, Num);//圖文消息的內容
  SetLength(G, Num);//自定義的圖文消息結構體
  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]));//調用圖文消息的內容,需提前寫好
        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;
//返回圖文消息的media_id備用

3、預覽上傳的圖文消息

const
PreviewUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=%s';

function GroupPreviewNews(OpenID, MediaID, AccessToken: String):TJSONObject;
var
  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、按組群發圖文消息

const
GroupSendUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=%s';

function GroupSendNews(GroupID, MediaID, AccessToken: String):TJSONObject;
var
  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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM