微信企业号开发:主动发送消息
2017-04-17 14:23
204 查看
主企业号主动发送消息,也就是企业号主动推送的消息,适合于企业的通知,通告等。因此如果公司有通知,要求通知到所有员工,就应该使用主动发送消息。
格式是json格式,而且微信很灵活,当touser,toparty,totag的json值是null时,微信服务器主动忽略了。原来还担心,如果是null,在生成json格式时如何忽略掉是null的字段。
核心基本类:
[csharp] view
plain copy
public class MsgBase
{
public MsgBase()
{
this.safe = "0"; //表示是否是保密消息,0表示否,1表示是,默认0
}
/// <summary>
/// UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
/// </summary>
public string touser { get; set; }
/// <summary>
/// PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数
/// </summary>
public string toparty { get; set; }
/// <summary>
/// TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数
/// </summary>
public string totag { get; set; }
/// <summary>
/// 消息类型
/// </summary>
public string msgtype { get; set; }
/// <summary>
/// 企业应用的id,整型。可在应用的设置页面查看
/// </summary>
public int agentid { get; set; }
/// <summary>
/// 表示是否是保密消息,0表示否,1表示是,默认0
/// </summary>
public string safe { get; set; }
}
[csharp] view
plain copy
public static class MsgType
{
public enum MsgBaseEnum
{
Text = 1,
image = 2,
voice = 3,
video = 4,
file = 5,
news = 6,
mpnews =7
};
public static string GetMsgTypeText(MsgBaseEnum type)
{
string text = "";
switch(type)
{
case MsgBaseEnum.Text:
text = "text";
break;
case MsgBaseEnum.image:
text = "image";
break;
case MsgBaseEnum.voice:
text = "voice";
break;
case MsgBaseEnum.video:
text = "video";
break;
case MsgBaseEnum.file:
text = "file";
break;
case MsgBaseEnum.news:
text = "news";
break;
case MsgBaseEnum.mpnews:
text = "mpnews";
break;
default:
throw new Exception("type=" + type + ",此类型的消息没有实现");
}
return text;
}
}
文字类型的消息
[csharp] view
plain copy
public class TextMsg : MsgBase
{
public TextMsg(string content)
{
this.text = new TextMsgContent(content);
this.msgtype = MsgType.GetMsgTypeText(MsgType.MsgBaseEnum.Text);
}
public TextMsgContent text { get; set; }
}
public class TextMsgContent
{
public TextMsgContent(string content)
{
this.content = content;
}
public string content { get; set; }
}
发送消息
[csharp] view
plain copy
public static class BLLMsg
{
public static bool SendMessage(MsgBase data)
{
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}";
string accessToken = BLLAccessToken.GetAccessToken();
var url = string.Format(urlFormat, accessToken);
WebUtils ut = new WebUtils();
var postData = Tools.ToJsonString<MsgBase>(data);
//数据不用加密发送
LogInfo.Info("发送消息: " + postData);
string sendResult = ut.DoPost(url, postData);
SendMsgResult tempAccessTokenjson = Tools.JsonStringToObj<SendMsgResult>(sendResult);
if (tempAccessTokenjson.HasError())
{
LogInfo.Error("发送消息错误: " + Tools.ToJsonString<SendMsgResult>(tempAccessTokenjson));
return false;
}
return true;
}
}
格式是json格式,而且微信很灵活,当touser,toparty,totag的json值是null时,微信服务器主动忽略了。原来还担心,如果是null,在生成json格式时如何忽略掉是null的字段。
核心基本类:
[csharp] view
plain copy
public class MsgBase
{
public MsgBase()
{
this.safe = "0"; //表示是否是保密消息,0表示否,1表示是,默认0
}
/// <summary>
/// UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
/// </summary>
public string touser { get; set; }
/// <summary>
/// PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数
/// </summary>
public string toparty { get; set; }
/// <summary>
/// TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数
/// </summary>
public string totag { get; set; }
/// <summary>
/// 消息类型
/// </summary>
public string msgtype { get; set; }
/// <summary>
/// 企业应用的id,整型。可在应用的设置页面查看
/// </summary>
public int agentid { get; set; }
/// <summary>
/// 表示是否是保密消息,0表示否,1表示是,默认0
/// </summary>
public string safe { get; set; }
}
[csharp] view
plain copy
public static class MsgType
{
public enum MsgBaseEnum
{
Text = 1,
image = 2,
voice = 3,
video = 4,
file = 5,
news = 6,
mpnews =7
};
public static string GetMsgTypeText(MsgBaseEnum type)
{
string text = "";
switch(type)
{
case MsgBaseEnum.Text:
text = "text";
break;
case MsgBaseEnum.image:
text = "image";
break;
case MsgBaseEnum.voice:
text = "voice";
break;
case MsgBaseEnum.video:
text = "video";
break;
case MsgBaseEnum.file:
text = "file";
break;
case MsgBaseEnum.news:
text = "news";
break;
case MsgBaseEnum.mpnews:
text = "mpnews";
break;
default:
throw new Exception("type=" + type + ",此类型的消息没有实现");
}
return text;
}
}
文字类型的消息
[csharp] view
plain copy
public class TextMsg : MsgBase
{
public TextMsg(string content)
{
this.text = new TextMsgContent(content);
this.msgtype = MsgType.GetMsgTypeText(MsgType.MsgBaseEnum.Text);
}
public TextMsgContent text { get; set; }
}
public class TextMsgContent
{
public TextMsgContent(string content)
{
this.content = content;
}
public string content { get; set; }
}
发送消息
[csharp] view
plain copy
public static class BLLMsg
{
public static bool SendMessage(MsgBase data)
{
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}";
string accessToken = BLLAccessToken.GetAccessToken();
var url = string.Format(urlFormat, accessToken);
WebUtils ut = new WebUtils();
var postData = Tools.ToJsonString<MsgBase>(data);
//数据不用加密发送
LogInfo.Info("发送消息: " + postData);
string sendResult = ut.DoPost(url, postData);
SendMsgResult tempAccessTokenjson = Tools.JsonStringToObj<SendMsgResult>(sendResult);
if (tempAccessTokenjson.HasError())
{
LogInfo.Error("发送消息错误: " + Tools.ToJsonString<SendMsgResult>(tempAccessTokenjson));
return false;
}
return true;
}
}
相关文章推荐
- 微信企业号开发:主动发送消息
- 微信企业号开发三:主动调用模式之发送news消息
- 微信企业号开发之发送消息问题
- 微信企业号开发(6)--发送消息
- Java企业微信开发_04_消息推送之发送消息(主动)
- 微信开发 获取Token、主动给企业成员发送消息
- Python微信企业号开发之回调模式接收微信端客户端发送消息及被动返回消息示例
- 微信企业号开发—发送消息
- C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)
- java微信企业号开发之发送消息(文本、图片、语音)
- 微信企业号开发—发送消息(文本、图片、语音)
- C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)
- JAVA微信开发:如何实现主动发送微信消息
- java微信企业号开发之发送消息(文本、图片、语音)
- 【fastweixin框架教程6】微信企业号给关注的用户主动发送推送消息
- JAVA微信开发:[20]如何实现主动发送微信消息
- 微信企业号开发—发送消息(文本、图片、语音)
- Java企业微信开发_05_消息推送之发送消息(主动)
- 微信公共服务平台开发(.Net 的实现)3-------发送文本消息
- 微信公众平台开发(73) 发送客服消息