c# - ASP.NET MVC 与 LACRM Api 的集成
问题描述
我之前在另一篇文章中问过这个问题,但我没有收到回复。我正在尝试将 ASP.Net MVC 与 Less Annoying CRM API 集成。目的是将表单提交存储到 CRM 中,以便将每个提交组织到类别或任务中。LACRM api 是为 PHP 设计的,开发人员提到他们缺乏在 C# 方面提供帮助的技能。这是我的代码(LACRM 使用默认字段“FullName”和“Email”,然后它有一个参数“CustomFields”,以便可以创建自定义表单输入。我遇到的问题是只有 FullName 被注册并且“Email和“CustomFields 显示在 Api 日志中,但未在 crm 上的字段中注册):我的代码:
public async Task createContact(Booking booking) { //string APITOKEN = "一些令牌"; //字符串用户代码 = "98992"; //字符串函数=“创建联系人”; //string PipeLineId = "3687195324475747612076041165694"; //string url = "https://api.lessannoyingcrm.com?APIToken=" + APITOKEN + "&UserCode=" + UserCode + "&Function=" + Function + "&PipelineId=" + PipeLineId + ""; 字符串 url = "https://api.lessannoyingcrm.com?APIToken=sometoken&UserCode=A2575&Function=CreateContact&PipelineId=3727019319364096972431828675722"; var postData = 新列表> { }; postData.Add(新键值对 ( "Email[1]", (new KeyValuePair("Text", booking.Email), 新的 KeyValuePair("Type", "Work")).ToString() )); postData.Add(新键值对 ( "Phone[1]", (new KeyValuePair("Text", booking.Phone), 新的 KeyValuePair("Type", "Work")).ToString() )); postData.Add(新键值对 ( "网站[0]", (new KeyValuePair("Text", "")).ToString() )); postData.Add(新键值对 ( “地点”, ( 新的 KeyValuePair("", booking.PostCode) ).ToString() )); postData.Add(新键值对 ( "自定义字段", ( new KeyValuePair("Conservatory", booking.Consevatory), 新的 KeyValuePair("尺寸", booking.Size), new KeyValuePair("类型", booking.RoofType), 新的 KeyValuePair(“来源”,booking.HearAboutUs), new KeyValuePair("评论", booking.OtherInfo), new KeyValuePair("Conservatory", booking.SelectedProduct) ).ToString() )); 使用 (var httpClient = new HttpClient()) { 使用 (var content = new FormUrlEncodedContent(postData)) { 内容.Headers.Clear(); content.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); HttpResponseMessage 响应 = 等待 httpClient.PostAsync(url, content); var apiresponse = 等待 response.Content.ReadAsAsync(); } } 返回真; }
这是 LACRM PHP 代码:
$_REQUEST['ContactName'], “电子邮件”=>数组( 0=>数组( "文本"=>"$_REQUEST[电子邮件]", “类型”=>“工作” ) ), “电话”=>数组( 0=>数组( "文本"=>"$_REQUEST[电话]", “类型”=>“工作” ) ), ); //...然后使用“CallAPI”函数将信息发送到LACRM $Result = CallAPI($UserCode, $APIToken, $Function, $Parameters); //就是这样,联系人现在将显示在 CRM 中! //现在让我们从 HTML 表单中输入“评论”字段作为联系人记录上的注释 //获取新的ContactId $ContactId = $Result['ContactId']; $Function = "创建笔记"; $参数=数组( "ContactId"=>$ContactId, "注意"=>$_REQUEST['评论'] ); //然后将该注释传递给API $Result = CallAPI($UserCode, $APIToken, $Function, $Parameters); /* 您可能还想在这里做各种其他事情,例如: - 设置任务跟进联系人(https://www.lessannoyingcrm.com/help/topic/API_Example_Code_PHP/11/) - 将联系人添加为潜在客户 (https://www.lessannoyingcrm.com/help/topic/API_Example_Code_PHP/87/) - 将联系人添加到组(https://www.lessannoyingcrm.com/help/topic/API_Example_Code_PHP/13/) - 给自己发送一封电子邮件,让您知道表单已提交(您可以使用 PHP“邮件”功能) */ //现在将访问者转发到一个 html 页面,确认我们获得了他们的联系信息 标头('位置:confirm.html'); /* 此函数获取调用 API 所需的所有设置并将它们放入 一个长 URL,然后它向该 URL 发出 HTTP 请求。 */ 函数调用API($UserCode, $APIToken, $Function, $Parameters){ $APIResult = file_get_contents("https://api.lessannoyingcrm.com?UserCode=$UserCode&APIToken=$APIToken&". "Function=$Function&Parameters=".urlencode(json_encode($Parameters))); $APIResult = json_decode($APIResult, true); if(@$APIResult['Success'] === true){ //echo "成功!"; } 别的{ echo "API 调用失败。错误:".@$APIResult['Error']; 出口; } 返回 $APIResult; }
我将非常感谢任何帮助使自定义字段正常工作。谢谢你
解决方案
在 C# 中,关联数组是Dictionary。您可以准备以下参数:
var parameters = new Dictionary<string, object> {
{ "FullName", booking.FullName },
{ "Email",
new object[] {
new Dictionary<string, string> {
{ "Text", booking.Email},
{ "Type", "Work"},
}
}
},
{ "Phone", new object[] {
new Dictionary<string, string> {
{ "Text", booking.Phone},
{ "Type", "Work"},
}
}
}
// Complete the other data
};
在 PHP 示例中,API 调用使用 HTTP 方法 GET,但您可以使用 POST(我提倡这样做)。从 LACRAM文档:
您可以使用 POST、GET 或任何其他您想要的(对我们来说都一样)。
string url = "https://api.lessannoyingcrm.com?APIToken=sometoken&UserCode=A2575&Function=CreateContact&PipelineId=3727019319364096972431828675722";
var parametersAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(parameters);
using (var httpClient = new HttpClient())
{
var response = await httpClient.PostAsync(url, new StringContent(parametersAsJson));
// Check the response status code to check if operation success
// Usualy 200 (Ok), maybe LACRAM return other success code, check the doc
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var resultAsJson = await response.Content.ReadAsStringAsync();
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(resultAsJson);
var contactId = result["ContactId"];
var companyId = result["CompanyId"];
}
else
{
throw new InvalidOperationException("Fail to create the contact");
}
}
推荐阅读
- python - 如何选择熊猫数据框中的第一行?
- c - 如何使用 exec-family 实现“查找”命令?
- ballerina - 运行芭蕾舞演员服务的问题
- reactjs - 即使您在 .then 中,Setstate 也会完成
- swift - SwiftUI NavigationView 不记得状态
- python - Python中graphviz模块遇到的问题 - Mac OSX
- c# - 将 WCF 服务请求参数更改为消息而不中断客户端
- python - 只获取 href beautifulsoup
- android - 弹出窗口使通知栏在 android 的全屏活动中可见
- flutter - 有没有办法在应用程序中打开 URL 而不是在浏览器中打开 URL?