hnxxcxg

微信公众平台——高级群发接口

微信公众账号在使用接口时,对多媒体文件、多媒体消息的获取和调用等操作,是通过media_id进行的。通过该接口,微信公众账号可以上传和下载多媒体文件。

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;

分类:

技术点:

相关文章: