Skip to content

Commit

Permalink
1.添加根据用户id发送的接口。
Browse files Browse the repository at this point in the history
2.添加返回值。
  • Loading branch information
majian159 committed Sep 15, 2015
1 parent 9e38d05 commit d1e6567
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 14 deletions.
10 changes: 8 additions & 2 deletions Rabbit.WeiXin/Rabbit.WeiXin.Tests/GroupMessageServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Rabbit.WeiXin.Tests.Utility;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rabbit.WeiXin.MP.Api.GroupMessage;
using Rabbit.WeiXin.MP.Api.Material;
using Rabbit.WeiXin.Tests.Utility;

namespace Rabbit.WeiXin.Tests
{
Expand Down Expand Up @@ -93,6 +93,12 @@ public void SendCardByGroupTest()
_groupMessageService.SendByGroup(new GroupFilter(), new GroupMessageCard("cardId"));
}

[TestMethod]
public void SendTextByGroupFromOpenIdTest()
{
_groupMessageService.SendByUsers(new[] { OpenId }, new GroupMessageText("content"));
}

#endregion Test Method
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Rabbit.WeiXin.Utility;
using System;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace Rabbit.WeiXin.MP.Api.GroupMessage
{
Expand All @@ -16,7 +17,16 @@ public interface IGroupMessageService
/// </summary>
/// <param name="filter">群组的筛选信息。</param>
/// <param name="message">群组消息。</param>
void SendByGroup(GroupFilter filter, GroupMessage message);
/// <returns>发送结果。</returns>
GroupSendResult SendByGroup(GroupFilter filter, GroupMessage message);

/// <summary>
/// 发送群组消息。
/// </summary>
/// <param name="userOpenIds">用户Id数组。</param>
/// <param name="message">群组消息。</param>
/// <returns>发送结果。</returns>
GroupSendResult SendByUsers(string[] userOpenIds, GroupMessage message);
}

/// <summary>
Expand Down Expand Up @@ -50,10 +60,9 @@ public GroupMessageService(AccountModel accountModel)
/// </summary>
/// <param name="filter">群组的筛选信息。</param>
/// <param name="message">群组消息。</param>
public void SendByGroup(GroupFilter filter, GroupMessage message)
/// <returns>发送结果。</returns>
public GroupSendResult SendByGroup(GroupFilter filter, GroupMessage message)
{
var url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=" + _accountModel.GetAccessToken();

object filterObj;

if (filter.GroupId.HasValue)
Expand All @@ -65,6 +74,34 @@ public void SendByGroup(GroupFilter filter, GroupMessage message)
filterObj = new { is_to_all = true };
}

return SendMessage(message, obj =>
{
obj["filter"] = JObject.Parse(JsonConvert.SerializeObject(filterObj));
});
}

/// <summary>
/// 发送群组消息。
/// </summary>
/// <param name="userOpenIds">用户Id数组。</param>
/// <param name="message">群组消息。</param>
/// <returns>发送结果。</returns>
public GroupSendResult SendByUsers(string[] userOpenIds, GroupMessage message)
{
return SendMessage(message, obj =>
{
obj["touser"] = JArray.Parse(JsonConvert.SerializeObject(userOpenIds));
});
}

#endregion Implementation of IGroupMessageService

#region Private Method

private GroupSendResult SendMessage(GroupMessage message, Action<JObject> action)
{
var url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=" + _accountModel.GetAccessToken();

string msgtype;

switch (message.Type)
Expand Down Expand Up @@ -110,22 +147,38 @@ public void SendByGroup(GroupFilter filter, GroupMessage message)
throw new NotSupportedException("不支持的消息类型:" + message.Type);
}

var postJson = JsonConvert.SerializeObject(new
{
filter = filterObj,
msgtype,
});
var postObj = JObject.Parse(postJson);
var postObj = new JObject();
action(postObj);
postObj["msgtype"] = msgtype;
postObj[msgtype] = JObject.Parse(JsonConvert.SerializeObject(message));

WeiXinHttpHelper.Post(url, postObj.ToString());
var content = WeiXinHttpHelper.PostString(url, Encoding.UTF8.GetBytes(postObj.ToString()));
return JsonConvert.DeserializeObject<GroupSendResult>(content);
}

#endregion Implementation of IGroupMessageService
#endregion Private Method
}

#region Help Class

/// <summary>
/// 群组消息发送结果。
/// </summary>
public sealed class GroupSendResult
{
/// <summary>
/// 消息发送任务的ID。
/// </summary>
[JsonProperty("msg_id")]
public long MessageId { get; set; }

/// <summary>
/// 消息的数据ID,该字段只有在群发图文消息时,才会出现。可以用于在图文分析数据接口中,获取到对应的图文消息的数据,是图文分析数据接口中的msgid字段中的前半部分,详见图文分析数据接口中的msgid字段的介绍。
/// </summary>
[JsonProperty("msg_data_id")]
public long MessageDataId { get; set; }
}

/// <summary>
/// 群组筛选器。
/// </summary>
Expand Down

0 comments on commit d1e6567

Please sign in to comment.