Skip to content

Commit

Permalink
Merge pull request #2971 from JeffreySu/Developer
Browse files Browse the repository at this point in the history
Developer
  • Loading branch information
JeffreySu authored Dec 11, 2023
2 parents d8e59ad + e8bbd31 commit 2f757e5
Show file tree
Hide file tree
Showing 4 changed files with 384 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Samples/All/notebook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 使用说明
1. 安装`Visual Studio Code`
2. 安装并配置扩展`Polyglot Notebooks`
3. 修改`senparc.json`配置文件的内容
4. 打开`sample.ipynb`文件修改`注册盛派SDK`节中的json文件的绝对路径
5. 视个人需求调整代码
6. Enjoy!!!
351 changes: 351 additions & 0 deletions Samples/All/notebook/sample.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,351 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 引用Nuget包"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Microsoft.Extensions.Caching.Memory, 6.0.0</span></li><li><span>Microsoft.Extensions.Configuration.Json, 6.0.0</span></li><li><span>Senparc.Weixin.Cache.Redis, 2.15.13</span></li><li><span>Senparc.Weixin.MP, 16.19.3</span></li><li><span>Senparc.Weixin.WxOpen, 3.16.3</span></li></ul></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#r \"nuget:Senparc.Weixin.WxOpen\"\n",
"#r \"nuget:Senparc.Weixin.Cache.Redis\"\n",
"#r \"nuget:Senparc.Weixin.MP\"\n",
"#r \"nuget:Microsoft.Extensions.Caching.Memory,6.0.0\"\n",
"#r \"nuget:Microsoft.Extensions.Configuration.Json,6.0.0\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 引用命名空间"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"using Senparc.CO2NET;\n",
"using Senparc.CO2NET.RegisterServices;\n",
"using Senparc.Weixin.Cache.Redis;\n",
"using Senparc.Weixin.Entities;\n",
"using Senparc.Weixin;\n",
"using Senparc.Weixin.MP;\n",
"using Senparc.Weixin.WxOpen;\n",
"using Microsoft.Extensions.DependencyInjection;\n",
"using Microsoft.Extensions.Configuration;\n",
"using Senparc.Weixin.WxOpen.AdvancedAPIs.Template;\n",
"using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 配置文件"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"var configBuilder = new ConfigurationBuilder();\n",
"// 此处需要使用配置文件的绝对路径\n",
"configBuilder.AddJsonFile(@\"D:\\PythonProjects\\senparc.json\", false, false);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 注册盛派SDK"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"// 关闭盛派APM\n",
"Senparc.CO2NET.APM.Config.EnableAPM = false;\n",
"// 不抛出异常\n",
"Senparc.Weixin.Config.ThrownWhenJsonResultFaild = false;\n",
"\n",
"var config = configBuilder.Build();\n",
"\n",
"var senparcSetting = new SenparcSetting();\n",
"var senparcWeixinSetting = new SenparcWeixinSetting();\n",
"\n",
"config.GetSection(\"SenparcSetting\").Bind(senparcSetting);\n",
"config.GetSection(\"SenparcWeixinSetting\").Bind(senparcWeixinSetting);\n",
"\n",
"var services = new ServiceCollection();\n",
"services.AddSenparcGlobalServices(config);//Senparc.CO2NET 全局注册\n",
"\n",
"IRegisterService register = RegisterService.Start(senparcSetting)\n",
" .UseSenparcGlobal();\n",
"\n",
"var redisConfigurationStr = senparcSetting.Cache_Redis_Configuration;\n",
"Senparc.CO2NET.Cache.Redis.Register.SetConfigurationOption(redisConfigurationStr);\n",
"//键值对缓存策略(推荐)\n",
"Senparc.CO2NET.Cache.Redis.Register.UseKeyValueRedisNow();\n",
"\n",
"register.UseSenparcWeixinCacheRedis();\n",
"\n",
"register.UseSenparcWeixin(senparcWeixinSetting)\n",
" .RegisterMpAccount(senparcWeixinSetting, \"梧州珍宝巴士\")\n",
" .RegisterWxOpenAccount(senparcWeixinSetting, \"梧州公交\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 发送统一服务消息"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"data": {
"text/html": [
"<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>WxJsonResult:{errcode:&#39;45109&#39;,errcode_name:&#39;45109&#39;,errmsg:&#39;This API has been unsupported. For more details, please view https://developers.weixin.qq.com/community/develop/doc/000ae8d6348af08e7030bc2546bc01?blockType=1 rid: 650b8665-65fe50fc-076f7520&#39;}</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>errcode</td><td><span>45109</span></td></tr><tr><td>ErrorCodeValue</td><td><div class=\"dni-plaintext\"><pre>45109</pre></div></td></tr><tr><td>errmsg</td><td><div class=\"dni-plaintext\"><pre>This API has been unsupported. For more details, please view https://developers.weixin.qq.com/community/develop/doc/000ae8d6348af08e7030bc2546bc01?blockType=1 rid: 650b8665-65fe50fc-076f7520</pre></div></td></tr><tr><td>P2PData</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr></tbody></table></div></details><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"public static readonly string WxOpenAppId = Senparc.Weixin.Config.SenparcWeixinSetting.WxOpenAppId;// 与微信小程序后台的AppId设置保持一致,区分大小写。\n",
"public static readonly string WxOpenAppSecret = Senparc.Weixin.Config.SenparcWeixinSetting.WxOpenAppSecret;// 与微信小程序账号后台的AppId设置保持一致,区分大小写。\n",
"public static readonly string WeixinAppId = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId;// 微信公众号AppId\n",
"public static readonly string WeixinAppSecret = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppSecret;// 微信公众号AppId\n",
"private const string TemplateId = \"OJ-cY-IJhkw8SF_l0OZK_Cy5cKGN-uqhMiSXLxCaON8\";// 模板Id\n",
"const string openid = \"oMr9M5SoFzyGqjHm2pvUImj1w0BQ\";// 接收测试消息的openid\n",
"\n",
"UniformSendData sendData = new(openid,\n",
" new Mp_Template_Msg(WeixinAppId, TemplateId, \"https://mp.weixin.qq.com/s/ObnyNhpOIjij7zgS1RIvaA\",\n",
" new Miniprogram_Page(WxOpenAppId, string.Empty),\n",
" new\n",
" {\n",
" keyword1 = new TemplateDataItem(\"3075309865\"),\n",
" keyword2 = new TemplateDataItem(\"2023-09-10 17:02:22\"),\n",
" keyword3 = new TemplateDataItem(\"49路\"),\n",
" keyword4 = new TemplateDataItem(\"010678\"),\n",
" keyword5 = new TemplateDataItem(\"2.00\")\n",
" }));\n",
"\n",
"var result = TemplateApi.UniformSend(WxOpenAppId, sendData);\n",
"// 查看返回结果\n",
"result.Display();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 发送模板消息"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"data": {
"text/html": [
"<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>WxJsonResult:{errcode:&#39;40003&#39;,errcode_name:&#39;不合法的OpenID&#39;,errmsg:&#39;invalid openid rid: 650b8666-127fe826-7727fb1e&#39;}</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>msgid</td><td><div class=\"dni-plaintext\"><pre>0</pre></div></td></tr><tr><td>errcode</td><td><span>不合法的OpenID</span></td></tr><tr><td>ErrorCodeValue</td><td><div class=\"dni-plaintext\"><pre>40003</pre></div></td></tr><tr><td>errmsg</td><td><div class=\"dni-plaintext\"><pre>invalid openid rid: 650b8666-127fe826-7727fb1e</pre></div></td></tr><tr><td>P2PData</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr></tbody></table></div></details><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"var result = Senparc.Weixin.MP.AdvancedAPIs.TemplateApi.SendTemplateMessage(WeixinAppId, openid, TemplateId,\n",
" \"https://mp.weixin.qq.com/s/ObnyNhpOIjij7zgS1RIvaA\",\n",
" new\n",
" {\n",
" keyword1 = new TemplateDataItem(\"3075309865\"),\n",
" keyword2 = new TemplateDataItem(\"2023-09-10 17:02:22\"),\n",
" keyword3 = new TemplateDataItem(\"49路\"),\n",
" keyword4 = new TemplateDataItem(\"010678\"),\n",
" keyword5 = new TemplateDataItem(\"2.00\")\n",
" });\n",
"result.Display();"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "python"
},
"orig_nbformat": 4,
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
21 changes: 21 additions & 0 deletions Samples/All/notebook/senparc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
//CO2NET 设置
"SenparcSetting": {
//以下为 CO2NET 的 SenparcSetting 全局配置,请勿修改 key,勿删除任何项

"IsDebug": false,
"DefaultCacheNamespace": "WeChatCache",

//分布式缓存
"Cache_Redis_Configuration": "127.0.0.1,password=[password],connectTimeout=1000,connectRetry=2,syncTimeout=10000,defaultDatabase=3", //密码及其他配置
"SenparcUnionAgentKey": "WeChat" //SenparcUnionAgentKey
},
"SenparcWeixinSetting": {
"Token": "[Token]",
"EncodingAESKey": "[EncodingAESKey]",
"WeixinAppId": "[WeixinAppId]",
"WeixinAppSecret": "[WeixinAppSecret]",
"WxOpenAppId": "[WxOpenAppId]",
"WxOpenAppSecret": "[WxOpenAppSecret]"
}
}
Loading

0 comments on commit 2f757e5

Please sign in to comment.