using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Security.Cryptography; using System.Text; using System.Web; namespace Import_and_export.Utils { public class RongCloudMS { #region private string appKey; string appSecret; string apiUrl = "http://api.cn.ronghub.com"; public RongCloudMS(string appKey, string appSecret) { this.appKey = appKey; this.appSecret = appSecret; } #endregion #region class model 返回类属性和消息类型属性 //注册用户返回值类型 /// <summary> /// 注册用户返回值数据类型 /// </summary> public class TokenReslut { /// <summary> /// 返回码,200为正常 /// </summary> [JsonProperty] int code; /// <summary> /// 用户Token,可以保存在应用内。 /// </summary> [JsonProperty] String token; /// <summary> /// 用户Id,与输入的用户Id相同 /// </summary> [JsonProperty] String userId; /// <summary> /// 错误信息 /// </summary> [JsonProperty] String errorMessage; public TokenReslut(int code, String token, String userId, String errorMessage) { this.code = code; this.token = token; this.userId = userId; this.errorMessage = errorMessage; } public void SetCode(int code) { this.code = code; } public int GetCode() { return code; } public void SetToken(String token) { this.token = token; } public String getToken() { return token; } public void SetUserId(String userId) { this.userId = userId; } public String GetUserId() { return userId; } public void SetErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } public String GetErrorMessage() { return errorMessage; } public String toString() { return JsonConvert.SerializeObject(this); } } //接口返回值数据 /// <summary> /// 正常接口返回值状态类型 /// </summary> public class CodeSuccessReslut { [JsonProperty] int code; [JsonProperty] String errorMessage; public CodeSuccessReslut(int code, String errorMessage) { this.code = code; this.errorMessage = errorMessage; } public void setCode(int code) { this.code = code; } public int getCode() { return code; } public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } public String getErrorMessage() { return errorMessage; } public String toString() { return JsonConvert.SerializeObject(this); } } //文字消息 /// <summary> /// 文字消息类型 /// </summary> public class TxtMsgRC : SendMessage { [JsonProperty] private String content = ""; [JsonProperty] private String extra = ""; private string typeRC = "RC:TxtMsg"; public TxtMsgRC(String content, String extra) { this.content = content; this.extra = extra; } public string getType() { return typeRC; } public string toString() { JsonSerializerSettings jsetting = new JsonSerializerSettings(); jsetting.NullValueHandling = NullValueHandling.Ignore; return JsonConvert.SerializeObject(this); } } //语音消息 /// <summary> /// 语音消息类型 /// </summary> public class VcMsgRC : SendMessage { [JsonProperty] private String content = ""; [JsonProperty] private String extra = ""; [JsonProperty] private long duration = 0L; private string typeRC = "RC:VcMsg"; public VcMsgRC(String content, String extra, long duration) { this.content = content; this.extra = extra; this.duration = duration; } public string getType() { return typeRC; } public string toString() { JsonSerializerSettings jsetting = new JsonSerializerSettings(); jsetting.NullValueHandling = NullValueHandling.Ignore; return JsonConvert.SerializeObject(this); } } //图片消息 /// <summary> /// 图片消息 /// </summary> public class ImgMsgRC : SendMessage { [JsonProperty] private String content = ""; [JsonProperty] private String extra = ""; [JsonProperty] private String imageUri = ""; private string typeRC = "RC:ImgMsg"; public ImgMsgRC(String content, String extra, String imageUri) { this.content = content; this.extra = extra; this.imageUri = imageUri; } public string getType() { return typeRC; } public string toString() { JsonSerializerSettings jsetting = new JsonSerializerSettings(); jsetting.NullValueHandling = NullValueHandling.Ignore; return JsonConvert.SerializeObject(this); } } //图文消息 /// <summary> /// 图文消息 /// </summary> public class ImgTextMsgRC : SendMessage { [JsonProperty] private String content = ""; [JsonProperty] private String extra = ""; [JsonProperty] private String title = ""; [JsonProperty] private String imageUri = ""; [JsonProperty] private String url = ""; private string typeRC = "RC:ImgTextMsg"; public ImgTextMsgRC(String content, String extra, String title, String imageUri, String url) { this.content = content; this.extra = extra; this.title = title; this.imageUri = imageUri; this.url = url; } public string getType() { return typeRC; } public string toString() { JsonSerializerSettings jsetting = new JsonSerializerSettings(); jsetting.NullValueHandling = NullValueHandling.Ignore; return JsonConvert.SerializeObject(this); } } //位置消息 /// <summary> /// 位置消息 /// </summary> public class LBSMsgRC : SendMessage { [JsonProperty] private String content = ""; [JsonProperty] private String extra = ""; [JsonProperty] private double latitude = 0; [JsonProperty] private double longitude = 0; [JsonProperty] private String poi = ""; private string typeRC = "RC:LBSMsg"; public LBSMsgRC(String content, String extra, double latitude, double longitude, String poi) { this.content = content; this.extra = extra; this.latitude = latitude; this.longitude = longitude; this.poi = poi; } public string getType() { return typeRC; } public string toString() { JsonSerializerSettings jsetting = new JsonSerializerSettings(); jsetting.NullValueHandling = NullValueHandling.Ignore; return JsonConvert.SerializeObject(this); } } //文件消息 /// <summary> /// 文件消息 /// </summary> public class FileMsgRC : SendMessage { [JsonProperty] private String name = ""; [JsonProperty] private int size = 0; [JsonProperty] private String type = ""; [JsonProperty] private String fileUrl = ""; private string typeRC = "RC:FileMsg"; public FileMsgRC(string name, int size, string type, string fileUrl) { this.name = name; this.size = size; this.type = type; this.fileUrl = fileUrl; } public string getType() { return typeRC; } public string toString() { JsonSerializerSettings jsetting = new JsonSerializerSettings(); jsetting.NullValueHandling = NullValueHandling.Ignore; return JsonConvert.SerializeObject(this); } } //推送消息类型 public class TagObj { // 标签。(最多20个) [JsonProperty] String[] tag; // 如果填 userId 给 userId 发如果没有给 tag 发。(最多1000个) [JsonProperty] String[] userid; // true为全部,忽略上面的tag、userId。(必传) [JsonProperty] Boolean is_to_all; public TagObj(String[] tag, String[] userid, Boolean is_to_all) { this.tag = tag; this.userid = userid; this.is_to_all = is_to_all; } /** * 设置tag * */ public void setTag(String[] tag) { this.tag = tag; } /** * 获取tag * * @return String[] */ public String[] getTag() { return tag; } /** * 设置userid * */ public void setUserid(String[] userid) { this.userid = userid; } /** * 获取userid * * @return String[] */ public String[] getUserid() { return userid; } /** * 设置is_to_all * */ public void setIs_to_all(Boolean is_to_all) { this.is_to_all = is_to_all; } /** * 获取is_to_all * * @return Boolean */ public Boolean getIs_to_all() { return is_to_all; } public String toString() { return JsonConvert.SerializeObject(this); } } public class MsgObj { // push 消息中的消息体。 [JsonProperty] String content; // 聊天室名称。 [JsonProperty] String objectName; public MsgObj(String content, String objectName) { this.content = content; this.objectName = objectName; } /** * 设置content * */ public void setContent(String content) { this.content = content; } /** * 获取content * * @return String */ public String getContent() { return content; } /** * 设置objectName * */ public void setObjectName(String objectName) { this.objectName = objectName; } /** * 获取objectName * * @return String */ public String getObjectName() { return objectName; } public String toString() { return JsonConvert.SerializeObject(this); } } public class PlatformNotification { // 默认推送消息内容,如填写了 ios 或 android 下的 alert 时,则推送内容以对应平台系统的 alert 为准。(必传) [JsonProperty] String alert; // ios 或 android 不同平台下的附加信息,如果开发者自己需要,可以自己在 App 端进行解析。(非必传) [JsonProperty] Dictionary<String, String> extras; public PlatformNotification(String alert, Dictionary<String, String> extras) { this.alert = alert; this.extras = extras; } /** * 设置alert * */ public void setAlert(String alert) { this.alert = alert; } /** * 获取alert * * @return String */ public String getAlert() { return alert; } /** * 设置extras * */ public void setExtras(Dictionary<String, String> extras) { this.extras = extras; } /** * 获取extras * * @return Map<String,String> */ public Dictionary<String, String> getExtras() { return extras; } public String toString() { return JsonConvert.SerializeObject(this); } } public class Notification { // 默认推送消息内容,如填写了 ios 或 android 下的 alert 时,则推送内容以对应平台系统的 alert 为准。(必传) [JsonProperty] String alert; // 设置 iOS 平台下的推送及附加信息。 [JsonProperty] PlatformNotification ios; // 设置 Android 平台下的推送及附加信息。 [JsonProperty] PlatformNotification android; public Notification(String alert, PlatformNotification ios, PlatformNotification android) { this.alert = alert; this.ios = ios; this.android = android; } /** * 设置alert * */ public void setAlert(String alert) { this.alert = alert; } /** * 获取alert * * @return String */ public String getAlert() { return alert; } /** * 设置ios * */ public void setIos(PlatformNotification ios) { this.ios = ios; } /** * 获取ios * * @return PlatformNotification */ public PlatformNotification getIos() { return ios; } /** * 设置android * */ public void setAndroid(PlatformNotification android) { this.android = android; } /** * 获取android * * @return PlatformNotification */ public PlatformNotification getAndroid() { return android; } public String toString() { return JsonConvert.SerializeObject(this); } } public class PushMessage { // 目标操作系统。(iOS、Android)。(必传) [JsonProperty] String[] platform; // 发送人用户 Id。(必传) [JsonProperty] String fromuserid; // 推送条件,包括: tag 、 userid 、 is_to_all。(必传) [JsonProperty] TagObj audience; // true为全部,忽略上面的tag、userId。 [JsonProperty] MsgObj message; // 按操作系统类型推送消息内容,如 platform 中设置了给 ios 和 android 系统推送消息,而在 notification 中只设置了 ios 的推送内容,则 android 的推送内容为最初 alert 设置的内容。 [JsonProperty] Notification notification; public PushMessage(String[] platform, String fromuserid, TagObj audience, MsgObj message, Notification notification) { this.platform = platform; this.fromuserid = fromuserid; this.audience = audience; this.message = message; this.notification = notification; } /** * 设置platform * */ public void setPlatform(String[] platform) { this.platform = platform; } /** * 获取platform * * @return String[] */ public String[] getPlatform() { return platform; } /** * 设置fromuserid * */ public void setFromuserid(String fromuserid) { this.fromuserid = fromuserid; } /** * 获取fromuserid * * @return String */ public String getFromuserid() { return fromuserid; } /** * 设置audience * */ public void setAudience(TagObj audience) { this.audience = audience; } /** * 获取audience * * @return TagObj */ public TagObj getAudience() { return audience; } /** * 设置message * */ public void setMessage(MsgObj message) { this.message = message; } /** * 获取message * * @return MsgObj */ public MsgObj getMessage() { return message; } /** * 设置notification * */ public void setNotification(Notification notification) { this.notification = notification; } /** * 获取notification * * @return Notification */ public Notification getNotification() { return notification; } public String toString() { return JsonConvert.SerializeObject(this); } } #endregion //群信息查询返回结果 /// <summary> /// 群信息查询返回结果 /// </summary> public class GroupUserQueryReslut { // 返回码,200 为正常。 [JsonProperty] int code; // 群成员用户Id。 [JsonProperty] String id; // 群成员列表。 [JsonProperty] List<GroupUser> users; public GroupUserQueryReslut(int code, String id, List<GroupUser> users) { this.code = code; this.id = id; this.users = users; } /** * 设置code * */ public void setCode(int code) { this.code = code; } /** * 获取code * * @return Integer */ public int getCode() { return code; } /** * 设置id * */ public void setId(String id) { this.id = id; } /** * 获取id * * @return String */ public String getId() { return id; } /** * 设置users * */ public void setUsers(List<GroupUser> users) { this.users = users; } /** * 获取users * * @return List<GroupUser> */ public List<GroupUser> getUsers() { return users; } public String toString() { return JsonConvert.SerializeObject(this); } } //群成员信息 /// <summary> /// 群成员信息 /// </summary> public class GroupUser { // 用户 Id。 [JsonProperty] String id; public GroupUser(String id) { this.id = id; } /** * 设置id * */ public void setId(String id) { this.id = id; } /** * 获取id * * @return String */ public String getId() { return id; } public String toString() { return JsonConvert.SerializeObject(this); } } RongHttpClient _rongHttpClient = new RongHttpClient(); //注册用户 /// <summary> /// 注册用户 /// </summary> /// <param name="userId">用户Id</param> /// <param name="name">用户名称</param> /// <param name="portraitUri">用户头像</param> /// <returns></returns> public TokenReslut UserGetToken(string userId, string name, string portraitUri) { if (userId == null) { throw new ArgumentNullException("Paramer 'userId' is required"); } if (name == null) { throw new ArgumentNullException("Paramer 'name' is required"); } if (portraitUri == null) { throw new ArgumentNullException("Paramer 'portraitUri' is required"); } String postStr = ""; postStr += "userId=" + HttpUtility.UrlEncode(userId == null ? "" : userId, Encoding.UTF8) + "&"; postStr += "name=" + HttpUtility.UrlEncode(name == null ? "" : name, Encoding.UTF8) + "&"; postStr += "portraitUri=" + HttpUtility.UrlEncode(portraitUri == null ? "" : portraitUri, Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (TokenReslut)JsonConvert.DeserializeObject<TokenReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, apiUrl + "/user/getToken.json", postStr, "application/x-www-form-urlencoded")); } class RongHttpClient { public String ExecutePost(String appkey, String appSecret, String methodUrl, String postStr, String contentType) { Random rd = new Random(); int rd_i = rd.Next(); String nonce = Convert.ToString(rd_i); String timestamp = Convert.ToString(ConvertDateTimeInt(DateTime.Now)); String signature = GetHash(appSecret + nonce + timestamp); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(methodUrl); myRequest.Method = "POST"; if (contentType == null || contentType.Equals("") || contentType.Length < 10) { myRequest.ContentType = "application/x-www-form-urlencoded"; } else { myRequest.ContentType = contentType; } myRequest.ProtocolVersion = HttpVersion.Version10; myRequest.Headers.Add("App-Key", appkey); myRequest.Headers.Add("Nonce", nonce); myRequest.Headers.Add("Timestamp", timestamp); myRequest.Headers.Add("Signature", signature); myRequest.ReadWriteTimeout = 30 * 1000; byte[] data = Encoding.UTF8.GetBytes(postStr); myRequest.ContentLength = data.Length; Stream newStream = myRequest.GetRequestStream(); // Send the data. newStream.Write(data, 0, data.Length); newStream.Close(); return returnResult(myRequest); } /// <summary> /// DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="time"> DateTime时间格式</param> /// <returns>Unix时间戳格式</returns> public int ConvertDateTimeInt(System.DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); return (int)(time - startTime).TotalSeconds; } public String GetHash(String input) { //建立SHA1对象 SHA1 sha = new SHA1CryptoServiceProvider(); //将mystr转换成byte[] UTF8Encoding enc = new UTF8Encoding(); byte[] dataToHash = enc.GetBytes(input); //Hash运算 byte[] dataHashed = sha.ComputeHash(dataToHash); //将运算结果转换成string string hash = BitConverter.ToString(dataHashed).Replace("-", ""); return hash; } public string returnResult(HttpWebRequest myRequest) { HttpWebResponse myResponse = null; int httpStatus = 200; string content; try { myResponse = (HttpWebResponse)myRequest.GetResponse(); httpStatus = (int)myResponse.StatusCode; StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); content = reader.ReadToEnd(); } catch (WebException e) { //异常请求 myResponse = (HttpWebResponse)e.Response; httpStatus = (int)myResponse.StatusCode; using (Stream errData = myResponse.GetResponseStream()) { using (StreamReader reader = new StreamReader(errData)) { content = reader.ReadToEnd(); } } } return content; } } //发送个人消息 /// <summary> /// 发送个人消息 /// </summary> /// <param name="fromUserId">发送人用户 Id。(必传)</param> /// <param name="toUserId">接收用户 Id,可以实现向多人发送消息,每次上限为 1000 人。(必传)</param> /// <param name="message">消息。</param> /// <param name="pushContent"></param> /// <param name="pushData"></param> /// <param name="count"></param> /// <param name="verifyBlacklist"></param> /// <param name="isPersisted"></param> /// <param name="isCounted"></param> /// <param name="isIncludeSender"></param> /// <returns></returns> public CodeSuccessReslut PublishPrivate(String fromUserId, String[] toUserId, SendMessage message, String pushContent, String pushData, String count, int verifyBlacklist, int isPersisted, int isCounted, int isIncludeSender) { if (fromUserId == null) { throw new ArgumentNullException("Paramer 'fromUserId' is required"); } if (toUserId == null) { throw new ArgumentNullException("Paramer 'toUserId' is required"); } if (message.getType() == null) { throw new ArgumentNullException("Paramer 'ObjectName' is required"); } if (message.toString() == null) { throw new ArgumentNullException("Paramer 'Content' is required"); } String postStr = ""; postStr += "fromUserId=" + HttpUtility.UrlEncode(fromUserId == null ? "" : fromUserId, Encoding.UTF8) + "&"; for (int i = 0; i < toUserId.Length; i++) { String child = toUserId[i]; postStr += "toUserId=" + HttpUtility.UrlEncode(child, Encoding.UTF8) + "&"; } postStr += "objectName=" + HttpUtility.UrlEncode(message.getType(), Encoding.UTF8) + "&"; postStr += "content=" + HttpUtility.UrlEncode(message.toString(), Encoding.UTF8) + "&"; postStr += "pushContent=" + HttpUtility.UrlEncode(pushContent == null ? "" : pushContent, Encoding.UTF8) + "&"; postStr += "pushData=" + HttpUtility.UrlEncode(pushData == null ? "" : pushData, Encoding.UTF8) + "&"; postStr += "count=" + HttpUtility.UrlEncode(count == null ? "" : count, Encoding.UTF8) + "&"; postStr += "verifyBlacklist=" + HttpUtility.UrlEncode(Convert.ToString(verifyBlacklist) == null ? "" : Convert.ToString(verifyBlacklist), Encoding.UTF8) + "&"; postStr += "isPersisted=" + HttpUtility.UrlEncode(Convert.ToString(isPersisted) == null ? "" : Convert.ToString(isPersisted), Encoding.UTF8) + "&"; postStr += "isCounted=" + HttpUtility.UrlEncode(Convert.ToString(isCounted) == null ? "" : Convert.ToString(isCounted), Encoding.UTF8) + "&"; postStr += "isIncludeSender=" + HttpUtility.UrlEncode(Convert.ToString(isIncludeSender) == null ? "" : Convert.ToString(isIncludeSender), Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/message/private/publish.json", postStr, "application/x-www-form-urlencoded")); } //创建群组 /// <summary> /// 创建群组 /// </summary> /// <param name="userId">要加入群的用户Id</param> /// <param name="groupId">创建群组Id</param> /// <param name="groupName">群组Id对应的名称</param> /// <returns></returns> public CodeSuccessReslut CreateGroup(String[] userId, String groupId, String groupName) { if (userId == null) { throw new ArgumentNullException("Paramer 'userId' is required"); } if (groupId == null) { throw new ArgumentNullException("Paramer 'groupId' is required"); } if (groupName == null) { throw new ArgumentNullException("Paramer 'groupName' is required"); } String postStr = ""; for (int i = 0; i < userId.Length; i++) { String child = userId[i]; postStr += "userId=" + HttpUtility.UrlEncode(child, Encoding.UTF8) + "&"; } postStr += "groupId=" + HttpUtility.UrlEncode(groupId == null ? "" : groupId, Encoding.UTF8) + "&"; postStr += "groupName=" + HttpUtility.UrlEncode(groupName == null ? "" : groupName, Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/group/create.json", postStr, "application/x-www-form-urlencoded")); } //加入群组 /// <summary> /// 加入群组 /// </summary> /// <param name="userId">要加入群组的用户Id</param> /// <param name="groupId">群Id</param> /// <param name="groupName">群名称</param> /// <returns></returns> public CodeSuccessReslut JoinGroup(String[] userId, String groupId, String groupName) { if (userId == null) { throw new ArgumentNullException("Paramer 'userId' is required"); } if (groupId == null) { throw new ArgumentNullException("Paramer 'groupId' is required"); } if (groupName == null) { throw new ArgumentNullException("Paramer 'groupName' is required"); } String postStr = ""; for (int i = 0; i < userId.Length; i++) { String child = userId[i]; postStr += "userId=" + HttpUtility.UrlEncode(child, Encoding.UTF8) + "&"; } postStr += "groupId=" + HttpUtility.UrlEncode(groupId == null ? "" : groupId, Encoding.UTF8) + "&"; postStr += "groupName=" + HttpUtility.UrlEncode(groupName == null ? "" : groupName, Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/group/join.json", postStr, "application/x-www-form-urlencoded")); } //退出群组 /// <summary> /// 退出群组 /// </summary> /// <param name="userId">用户Id</param> /// <param name="groupId">群组Id</param> /// <returns></returns> public CodeSuccessReslut QuitGroup(String[] userId, String groupId) { if (userId == null) { throw new ArgumentNullException("Paramer 'userId' is required"); } if (groupId == null) { throw new ArgumentNullException("Paramer 'groupId' is required"); } String postStr = ""; for (int i = 0; i < userId.Length; i++) { String child = userId[i]; postStr += "userId=" + HttpUtility.UrlEncode(child, Encoding.UTF8) + "&"; } postStr += "groupId=" + HttpUtility.UrlEncode(groupId == null ? "" : groupId, Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/group/quit.json", postStr, "application/x-www-form-urlencoded")); } //发送群组消息 /// <summary> /// 发送群组消息 /// </summary> /// <param name="fromUserId">发送用户id</param> /// <param name="toGroupId">群组Id,可以多个群组</param> /// <param name="message">消息内容</param> /// <param name="pushContent">定义显示的 Push 内容,如果 objectName 为融云内置消息类型时,则发送后用户一定会收到 Push 信息. 如果为自定义消息,则 pushContent 为自定义消息显示的 Push 内容,如果不传则用户不会收到 Push 通知。(可选)</param> /// <param name="pushData">针对 iOS 平台为 Push 通知时附加到 payload 中,Android 客户端收到推送消息时对应字段名为 pushData。(可选)</param> /// <param name="isPersisted">当前版本有新的自定义消息,而老版本没有该自定义消息时,老版本客户端收到消息后是否进行存储,0 表示为不存储、 1 表示为存储,默认为 1 存储消息。(可选)</param> /// <param name="isCounted"></param> /// <param name="isIncludeSender"></param> /// <returns></returns> public CodeSuccessReslut PublishGroup(String fromUserId, String[] toGroupId, TxtMsgRC message, String pushContent, String pushData, int isPersisted, int isCounted, int isIncludeSender) { if (fromUserId == null) { throw new ArgumentNullException("Paramer 'fromUserId' is required"); } if (toGroupId == null) { throw new ArgumentNullException("Paramer 'toGroupId' is required"); } if (message.getType() == null) { throw new ArgumentNullException("Paramer 'ObjectName' is required"); } if (message.toString() == null) { throw new ArgumentNullException("Paramer 'Content' is required"); } String postStr = ""; postStr += "fromUserId=" + HttpUtility.UrlEncode(fromUserId == null ? "" : fromUserId, Encoding.UTF8) + "&"; for (int i = 0; i < toGroupId.Length; i++) { String child = toGroupId[i]; postStr += "toGroupId=" + HttpUtility.UrlEncode(child, Encoding.UTF8) + "&"; } postStr += "objectName=" + HttpUtility.UrlEncode(message.getType(), Encoding.UTF8) + "&"; postStr += "content=" + HttpUtility.UrlEncode(message.toString(), Encoding.UTF8) + "&"; postStr += "pushContent=" + HttpUtility.UrlEncode(pushContent == null ? "" : pushContent, Encoding.UTF8) + "&"; postStr += "pushData=" + HttpUtility.UrlEncode(pushData == null ? "" : pushData, Encoding.UTF8) + "&"; postStr += "isPersisted=" + HttpUtility.UrlEncode(Convert.ToString(isPersisted) == null ? "" : Convert.ToString(isPersisted), Encoding.UTF8) + "&"; postStr += "isCounted=" + HttpUtility.UrlEncode(Convert.ToString(isCounted) == null ? "" : Convert.ToString(isCounted), Encoding.UTF8) + "&"; postStr += "isIncludeSender=" + HttpUtility.UrlEncode(Convert.ToString(isIncludeSender) == null ? "" : Convert.ToString(isIncludeSender), Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/message/group/publish.json", postStr, "application/x-www-form-urlencoded")); } //加入黑名单 /// <summary> /// 加入黑名单 /// </summary> /// <param name="userId">用户Id</param> /// <param name="blackUserId">被加入黑名单的用户Id</param> /// <returns></returns> public CodeSuccessReslut AddBlackList(String userId, String blackUserId) { if (userId == null) { throw new ArgumentNullException("Paramer 'userId' is required"); } if (blackUserId == null) { throw new ArgumentNullException("Paramer 'blackUserId' is required"); } String postStr = ""; postStr += "userId=" + HttpUtility.UrlEncode(userId == null ? "" : userId, Encoding.UTF8) + "&"; postStr += "blackUserId=" + HttpUtility.UrlEncode(blackUserId == null ? "" : blackUserId, Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/user/blacklist/add.json", postStr, "application/x-www-form-urlencoded")); } //移出黑名单 /// <summary> /// 移除黑名单 /// </summary> /// <param name="userId">用户Id</param> /// <param name="blackUserId">被移除用户的Id</param> /// <returns></returns> public CodeSuccessReslut RemoveBlackList(String userId, String blackUserId) { if (userId == null) { throw new ArgumentNullException("Paramer 'userId' is required"); } if (blackUserId == null) { throw new ArgumentNullException("Paramer 'blackUserId' is required"); } String postStr = ""; postStr += "userId=" + HttpUtility.UrlEncode(userId == null ? "" : userId, Encoding.UTF8) + "&"; postStr += "blackUserId=" + HttpUtility.UrlEncode(blackUserId == null ? "" : blackUserId, Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/user/blacklist/remove.json", postStr, "application/x-www-form-urlencoded")); } //添加禁言群成员方法 /// <summary> /// 添加禁言群成员 /// </summary> /// <param name="userId">用户Id</param> /// <param name="groupId">群组Id</param> /// <param name="minute">禁言时长,以分钟为单位,最大值43200分钟</param> /// <returns></returns> public CodeSuccessReslut AddGagUserGroup(String userId, String groupId, String minute) { if (userId == null) { throw new ArgumentNullException("Paramer 'userId' is required"); } if (groupId == null) { throw new ArgumentNullException("Paramer 'groupId' is required"); } if (minute == null) { throw new ArgumentNullException("Paramer 'minute' is required"); } String postStr = ""; postStr += "userId=" + HttpUtility.UrlEncode(userId == null ? "" : userId, Encoding.UTF8) + "&"; postStr += "groupId=" + HttpUtility.UrlEncode(groupId == null ? "" : groupId, Encoding.UTF8) + "&"; postStr += "minute=" + HttpUtility.UrlEncode(minute == null ? "" : minute, Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/group/user/gag/add.json", postStr, "application/x-www-form-urlencoded")); } //移除禁言群成员方法 /// <summary> /// 移除禁言群成员 /// </summary> /// <param name="userId">用户Id,支持多个</param> /// <param name="groupId">群组Id</param> /// <returns></returns> public CodeSuccessReslut RollBackGagUserGroup(String[] userId, String groupId) { if (userId == null) { throw new ArgumentNullException("Paramer 'userId' is required"); } if (groupId == null) { throw new ArgumentNullException("Paramer 'groupId' is required"); } String postStr = ""; for (int i = 0; i < userId.Length; i++) { String child = userId[i]; postStr += "userId=" + HttpUtility.UrlEncode(child, Encoding.UTF8) + "&"; } postStr += "groupId=" + HttpUtility.UrlEncode(groupId == null ? "" : groupId, Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/group/user/gag/rollback.json", postStr, "application/x-www-form-urlencoded")); } //解散群组方法 /// <summary> /// 解散群组方法。(将该群解散,所有用户都无法再接收该群的消息。) /// </summary> /// <param name="userId">操作解散群的用户 Id。(必传)</param> /// <param name="groupId">要解散的群 Id。(必传)</param> /// <returns></returns> public CodeSuccessReslut DisMissGroup(String userId, String groupId) { if (userId == null) { throw new ArgumentNullException("Paramer 'userId' is required"); } if (groupId == null) { throw new ArgumentNullException("Paramer 'groupId' is required"); } String postStr = ""; postStr += "userId=" + HttpUtility.UrlEncode(userId == null ? "" : userId, Encoding.UTF8) + "&"; postStr += "groupId=" + HttpUtility.UrlEncode(groupId == null ? "" : groupId, Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/group/dismiss.json", postStr, "application/x-www-form-urlencoded")); } //查询群成员 /// <summary> /// 查询群成员 /// </summary> /// <param name="groupId">查询群Id</param> /// <returns></returns> public GroupUserQueryReslut QueryUser(String groupId) { if (groupId == null) { throw new ArgumentNullException("Paramer 'groupId' is required"); } String postStr = ""; postStr += "groupId=" + HttpUtility.UrlEncode(groupId == null ? "" : groupId, Encoding.UTF8) + "&"; postStr = postStr.Substring(0, postStr.LastIndexOf('&')); return (GroupUserQueryReslut)JsonConvert.DeserializeObject<GroupUserQueryReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/group/user/query.json", postStr, "application/x-www-form-urlencoded")); } //消息接口 /// <summary> /// 发送用户信息接口 /// </summary> public interface SendMessage { string getType(); string toString(); } //推送消息 /// <summary> /// 推送消息 /// </summary> /// <param name="pushMessage">消息类型内容</param> /// <returns></returns> public CodeSuccessReslut BroadcastPush(PushMessage pushMessage) { if (pushMessage == null) { throw new ArgumentNullException("Paramer 'pushMessage' is required"); } String postStr = ""; postStr = JsonConvert.SerializeObject(pushMessage); return (CodeSuccessReslut)JsonConvert.DeserializeObject<CodeSuccessReslut>(_rongHttpClient.ExecutePost(appKey, appSecret, RongCloud.RONGCLOUDURI + "/push.json", postStr, "application/json")); } } }