双十一之前终于把淘宝客公众号搭建好了,虽然耗时不少,但还是蛮有成就感的,能不能赚钱就另外一回事了。
我这里用到了淘宝联盟api权限、拼多多平台接口api、京东申请比较麻烦就没有申请,所以京东使用了第三方免费接口,各联盟账号api权限申请我就不赘述了,有疑问的话可以在下面留言,或加我微信公众号“网购有券”联系我。
我这里有发布好的接口,可供大家调用,需要的话,可加我微信
淘宝找券实现
原理:
- 通过用户手淘分享的商品信息,信息中包含商品关键字和链接,使用商品关键字通过淘宝 通用物料搜索API 接口查询一批商品;
- 然后通过分享信息中的淘宝链接http抓包方式获取淘宝商品item id;
然后通过item id在查询的一批商品中找到指定的商品优惠券、佣金等信息。
public static string GetTaobaoCoupon(RequestMessageText responseMessageText) { string itemInfo = responseMessageText.Content.Trim(); string responeMessage = ""; try { Match m_title = Regex.Match(itemInfo, @"【.*】"); string temp = m_title.Value; if (!string.IsNullOrEmpty(temp)) { temp = temp.Substring(1, temp.Length - 2); } else { return ""; } if (temp.Contains("#手聚App")) { int IndexofA = temp.IndexOf("宝贝不错:"); int IndexofB = temp.IndexOf("(分享自"); temp = temp.Substring(IndexofA + 5, IndexofB - IndexofA - 5); } string title = temp; //通过商品关键字查询商品 ITopClient client = new DefaultTopClient(taobaoApiUrl,taobaoAppkey, taobaoSecret); TbkDgMaterialOptionalRequest req = new TbkDgMaterialOptionalRequest(); req.AdzoneId = addzoneId; req.Platform = 2L; req.PageSize = 100L; req.Q = title; req.PageNo = 1L; TbkDgMaterialOptionalResponse rsp = client.Execute(req); if (rsp.ResultList.Count > 0) { //获取淘宝短链接 Match m_url = Regex.Match(itemInfo, @"htt(p|ps):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?"); if (m_url.Value == "") { return responeMessage; } var s = HttpUtility.HttpGet(m_url.Value, "", "utf-8"); //Match am_url = Regex.Match(s, @"(?<=var url = ')(.*)(?=')"); //获取宝贝item id Match m_item = Regex.Match(s, @"(?<=m.taobao.com\/i)([0-9]*)"); string item_id = m_item.Value; if (string.IsNullOrEmpty(item_id)) { Match am_url = Regex.Match(s, @"(?<=var url = ')(.*)(?=')"); var htmlContent = HttpUtility.HttpGet(am_url.Value, "", "gbk"); Match re_m_item = Regex.Match(htmlContent, @"(?<=taobao.com/item.htm\?id=)([0-9]*)"); item_id = re_m_item.Value; } if (string.IsNullOrEmpty(item_id)) { //LogHelper.WriteLog(typeof(WechatController), "通过抓包方式未获取到宝贝item id"); var g = rsp.ResultList.Where(y => !string.IsNullOrEmpty(y.CouponId)).OrderByDescending(w => w.Volume).FirstOrDefault(); if (g == null) { responeMessage = ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231"); } else { var hongbao = (decimal.Parse(g.ZkFinalPrice) - decimal.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate; responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(g.ZkFinalPrice) - double.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{GetTaobaoKePassword(g.CouponShareUrl, g.PictUrl + "_400x400.jpg")}\n"; } return responeMessage; } else { float numid = 0; try { numid = float.Parse(item_id); } catch (Exception ex) { //通过淘宝链接没有获取到item id,显示销量最高商品 var g = rsp.ResultList.Where(y => !string.IsNullOrEmpty(y.CouponId)).OrderByDescending(y => y.Volume).FirstOrDefault(); var hongbao = (decimal.Parse(g.ZkFinalPrice) - decimal.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate; responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(g.ZkFinalPrice) - double.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{GetTaobaoKePassword(g.CouponShareUrl, g.PictUrl + "_400x400.jpg")}\n"; return responeMessage; } //在接口返回的商品中找查询的商品 foreach (var g in rsp.ResultList) { if (g.NumIid == numid) { if (string.IsNullOrEmpty(g.CouponInfo)) { var hongbao = decimal.Parse(g.ZkFinalPrice) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate; responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【约返利】{Math.Round(hongbao, 2)}元\n复制这条信息,打开「手机绹宝」领巻下单{GetTaobaoKePassword(g.Url, g.PictUrl + "_400x400.jpg")}\n==========================\n下单确认收货后就能收到返利佣金啦~\n 点击查看 <a href='http://mp.weixin.qq.com/s?__biz=Mzg2NTAxOTEyMA==&mid=100000146&idx=1&sn=62405c8df3db46e74940aefb9ac3737b&chksm=4e61340d7916bd1bf645afbc6d10c1f19561d7fa59847516c01e64c0791e6d544f4f56c4f498#rd'>如何领取返利</a>"; return responeMessage; } else { var hongbao = (decimal.Parse(g.ZkFinalPrice) - decimal.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate; responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(g.ZkFinalPrice) - double.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{GetTaobaoKePassword(g.CouponShareUrl, g.PictUrl + "_400x400.jpg")}\n"; return responeMessage; } } } //没有找到,有相似宝贝推荐 var w = rsp.ResultList.Where(y => !string.IsNullOrEmpty(y.CouponId)).OrderByDescending(y => y.Volume).FirstOrDefault(); if (w == null) { responeMessage = ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231"); } else { var hongbao = (decimal.Parse(w.ZkFinalPrice) - decimal.Parse(Regex.Match(w.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(w.CommissionRate) / 10000 * commission_rate; responeMessage = $"/:rose 亲,这款商品的优惠返利活动结束了~\n已为你推荐以下宝贝。\n==========================\n{w.Title}\n【在售价】{w.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(w.ZkFinalPrice) - double.Parse(Regex.Match(w.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{GetTaobaoKePassword(w.CouponShareUrl, w.PictUrl + "_400x400.jpg")}\n"; } return responeMessage; } } else { responeMessage = ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231"); } } catch (Exception ex) { //LogHelper.WriteLog(typeof(WechatController), "返回消息异常" + ex.Message); } return responeMessage; }
京东、拼多多找券
京东、拼多多找券都比较简单,微信支持京东、拼多多链接,可以通过链接获取到商品id,然后直接通过接口使用商品id找券。
京东代码:使用了第三方接口(好京客)public static string GetJDCoupon(RequestMessageText requestMessageText) { string msg = requestMessageText.Content; Match m_goods = Regex.Match(msg, @"(?<=product\/)([0-9]*)|(?<=sku=)([0-9]*)"); string skuId = m_goods.Value; if (string.IsNullOrEmpty(skuId)) { // LogHelper.WriteLog(typeof(WechatController), "获取京东skuid失败" + msg); return ""; } try { var hjkGoodsDetail = hjkApi.GetJDGoodsDetail(skuId); if (hjkGoodsDetail.StatusCode == 200 && hjkGoodsDetail.Data != null && !string.IsNullOrEmpty(hjkGoodsDetail.Data.CouponList)) { var model = hjkApi.GetUnionUrl(skuId, hjkGoodsDetail.Data.CouponList); if (model != null && model.StatusCode == 200 && !string.IsNullOrEmpty(model.Data)) { return $"{hjkGoodsDetail.Data.SkuName}\n【在售价】{hjkGoodsDetail.Data.WlPrice}元\n【巻后价】{hjkGoodsDetail.Data.WlPriceAfter} 元\n\n\ue231 <a href='{model.Data}'>点击这里领券下单</a>\n\n"; } else { return ConfigurationManager.AppSettings["jd_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231"); } } else { return ConfigurationManager.AppSettings["jd_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231"); } } catch (Exception ex) { //LogHelper.WriteLog(typeof(WechatController), "获取京东skuid失败" + ex.Message); return ""; } }
拼多多代码如下:
public static async Task<string> GetPDDCouponAsync(RequestMessageText responseMessageText) { PddCommonApi.ClientId = "d8172a66ddf14220beac58e8eddca0d9"; PddCommonApi.ClientSecret = "d5993ce16cc0d1bd35403176f35544962e786645"; PddCommonApi.RedirectUri = "RedirectUri"; PddCommonApi.AccessToken = ""; string msg = responseMessageText.Content; Match m_goods = Regex.Match(msg, @"(?<=goods_id=)([0-9]*)"); string goods_id = m_goods.Value; if (string.IsNullOrEmpty(goods_id)) { //LogHelper.WriteLog(typeof(WechatController), "获取拼多多goods id失败" + msg); return ""; } DdkApi api = new DdkApi(); var model = new DetailDdkGoodsRequestModel() { Type = "pdd.ddk.goods.detail", GoodsIdList = $"[{goods_id}]" }; DetailDdkGoodsResponseModel result = null; try { result = await api.DetailDdkGoodsAsync(model); } catch (Exception ex) { //LogHelper.WriteLog(typeof(WechatController), "调用拼多多获取商品详细信息失败" + ex.Message); return ""; } var goods = result.GoodsDetailResponse.GoodsDetails.FirstOrDefault(); if (goods == null) //无优惠券 无佣金 { return "/:rose 亲,这款商品的优惠返利活动结束了~\n请换个商品试试吧。\n========================\n\ue231 <a href='https://mobile.yangkeduo.com/duo_cms_mall.html?pid=2495191_31302208cpsSign=CM2495191_31302208_3a1c1a0431608b9c1eb417183d57c1bdduoduo_type=2'>拼多多优惠券商城</a>\n下单确认收货后就能收到返利佣金啦~"; } else if (goods.HasCoupon) //有优惠券 有佣金 { try { var promotionUrlModel = await api.GenerateDdkGoodsPromotionUrlAsync(new GenerateDdkGoodsPromotionUrlRequestModel { Type = "pdd.ddk.goods.promotion.url.generate", PId = pdd_pid, GoodsIdList = $"[{goods_id}]", GenerateShortUrl = true, CustomParameters = responseMessageText.FromUserName }); return $"/:rose 亲,商品信息如下~\n========================\n{goods.GoodsName}\n【在售价】{((decimal)goods.MinGroupPrice) / 100}元\n【券后价】{Math.Round(((decimal)(goods.MinGroupPrice - goods.CouponDiscount.Value)) / 100, 2)}元\n\ue231 <a href='{promotionUrlModel.GoodsPromotionUrlGenerateResponse.GoodsPromotionUrlList.FirstOrDefault().Url}'>点击这里下单</a>\n下单确认收货后就能收到返利佣金啦~"; //return $"/:rose 亲,商品信息如下~\n========================\n{goods.GoodsName}\n【在售价】{((decimal)goods.MinGroupPrice) / 100}元\n【券后价】{Math.Round(((decimal)(goods.MinGroupPrice - goods.CouponDiscount.Value)) / 100,2)}元\n【约返利】{Math.Round((decimal)((goods.MinNormalPrice - goods.CouponDiscount.Value) * goods.PromotionRate) / 100000,2)}元\n\ue231 <a href='{promotionUrlModel.GoodsPromotionUrlGenerateResponse.GoodsPromotionUrlList.FirstOrDefault().Url}'>点击这里下单</a>\n下单确认收货后就能收到返利佣金啦~"; } catch (Exception ex) { //LogHelper.WriteLog(typeof(WechatController), "调用拼多多获取推广链接失败" + ex.Message); return ""; } } else //无优惠券 但有佣金 { try { var promotionUrlModel = await api.GenerateDdkGoodsPromotionUrlAsync(new GenerateDdkGoodsPromotionUrlRequestModel { Type = "pdd.ddk.goods.promotion.url.generate", GoodsIdList = $"[{goods_id}]", PId = pdd_pid, GenerateShortUrl = true, CustomParameters = responseMessageText.FromUserName }); return $"/:rose 亲,商品信息如下~\n========================\n{goods.GoodsName}\n【在售价】{((decimal)goods.MinGroupPrice) / 100}元\n【约返利】{Math.Round((decimal)(goods.MinGroupPrice * goods.PromotionRate) / 100000, 2)}元\n\ue231 <a href='{promotionUrlModel.GoodsPromotionUrlGenerateResponse.GoodsPromotionUrlList.FirstOrDefault().Url}'>点击这里下单</a>\n下单确认收货后就能收到返利佣金啦~\n\n 点击查看 <a href='http://mp.weixin.qq.com/s?__biz=Mzg2NTAxOTEyMA==&mid=100000146&idx=1&sn=62405c8df3db46e74940aefb9ac3737b&chksm=4e61340d7916bd1bf645afbc6d10c1f19561d7fa59847516c01e64c0791e6d544f4f56c4f498#rd'>如何领取返利</a>"; } catch (Exception ex) { //LogHelper.WriteLog(typeof(WechatController), "调用拼多多获取推广链接失败" + ex.Message); return ""; } } }
完整代码 https://github.com/xuhang2015/TBK
请关注“网购有券”,支持下我吧。有任何问题也可以在公众号中联系我。
完整的代码下载完之后,怎么操作?放在哪里?