ASP.NET MVC与LACRM Api的集成



我以前在另一篇文章中问过这个问题,但没有得到回应。我正在尝试将ASP.NetMVC与不那么麻烦的CRMneneneba API进行集成。目的是将表单提交存储到CRM中,以便将每个提交组织为类别或任务。LACRMapi是为PHP设计的,开发人员提到他们缺乏在C#方面提供帮助的技能。这是我的代码(LACRM使用默认字段"FullName"one_answers"Email",然后它有一个参数"CustomFields",这样就可以创建自定义表单输入。我遇到的问题是,只有FullName被注册,"Email"one_answers"CustomField"显示在Api日志中,但没有在crm的字段中注册):我的代码:

公共异步任务createContact(预订){//string APITOKEN="某个令牌";//string用户代码="98992";//string函数="CreateContact";//string PipeLineId="3687195324474761207604165694";//字符串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(新KeyValuePair("电子邮件[1]",(新KeyValuePair("文本",booking.Email),new KeyValuePair("Type"、"Work")).ToString()));postData.Add(新KeyValuePair("电话[1]",(新KeyValuePair("文本",booking.Phone),new KeyValuePair("Type"、"Work")).ToString()));postData.Add(新KeyValuePair("网站[0]",(new KeyValuePair("Text",")).ToString()));postData.Add(新KeyValuePair("位置"(new KeyValuePair(",booking.PostCode)).ToString()));postData.Add(新KeyValuePair("自定义字段"(new KeyValuePair("Conservatory",预订.Conservatory),new KeyValuePair("Size",booking.Size),new KeyValuePair("Type",booking.RoofType),new KeyValuePair("Source",booking.HearAboutUs),new KeyValuePair("Comment",booking.OtherInfo),new KeyValuePair("Conservatory",预订.SelectedProduct)).ToString()));使用(var httpClient=new httpClient()){使用(var content=new FormUrlEncodedContent(postData)){所容纳之物Headers.Clear();所容纳之物Headers.Add("内容类型","application/x-www-form-urlencoded");HttpResponseMessage响应=等待httpClient.PostAsync(url,内容);var apiresponse=等待响应。Content.ReadAsAsync();}}返回true;}

这是LACRM的PHP代码:

$_REQUEST["联系人名称"],"电子邮件"=>阵列(0=>数组("Text"=>"$_REQUEST[电子邮件]","Type"=>"工作")),"电话"=>阵列(0=>数组("Text"=>"$_REQUEST[电话]","Type"=>"工作")),);//。。。然后使用"CallAPI"函数将信息发送到LACRM$Result=调用API($UserCode,$APIToken,$Function,$Parameters);//就是这样,联系人现在将显示在CRM中!//现在,让我们在HTML表单中输入"Comment"字段,作为联系人记录上的注释//获取新的ContactId$ContactId=$Result['ContactId'];$Function="CreateNote";$Parameters=数组("ContactId"=>$ContactId,"备注"=>$_REQUEST["备注"]);//并将注释传递给API$Result=调用API($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页面,确认我们已获得他们的联系信息header('location: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["成功"]===true){//echo"成功!";}其他{echo"API调用失败。错误:"@$APIResult["错误"];出口}return$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");
}
}