简体   繁体   English

ASP.NET MVC 与 LACRM Api 集成

[英]ASP.NET MVC integration with LACRM Api

I have asked this question before in another post, however I didn't received a respond.我之前在另一篇文章中问过这个问题,但我没有收到回复。 I am trying to integrate ASP.Net MVC with Less Annoying CRM API.我正在尝试将 ASP.Net MVC 与 Less Annoying CRM API 集成。 The purpose is to store form submissions into the CRM so as to organised each submissions into categories or tasks.目的是将表单提交存储到 CRM 中,以便将每个提交组织到类别或任务中。 The LACRM api is designed for PHP and the developers mentioned that they lack the skills to help in regards to C#. LACRM api 是为 PHP 设计的,开发人员提到他们缺乏帮助 C# 的技能。 Here is my code (The LACRM uses default fields "FullName"and "Email" then it have a parameter "CustomFields" so that custom form inputs can be created. The issue I am experiencing is that only FullName is getting registered and the "Email and "CustomFields are showing in the Api logs but not registering in the fields on the crm): My Code:这是我的代码(LACRM 使用默认字段“FullName”和“Email”,然后它有一个参数“CustomFields”,以便可以创建自定义表单输入。我遇到的问题是只有 FullName 被注册并且“Email和“CustomFields 显示在 Api 日志中,但未在 crm 上的字段中注册):我的代码:


    public async Task createContact(Booking booking)
        {
           
            //string APITOKEN = "some token";
            //string UserCode = "98992";
            //string Function = "CreateContact";
            //string PipeLineId = "3687195324475747612076041165694";
            //string url = "https://api.lessannoyingcrm.com?APIToken=" + APITOKEN + "&UserCode=" + UserCode + "&Function=" + Function + "&PipelineId=" + PipeLineId + "";
            string url = "https://api.lessannoyingcrm.com?APIToken=sometoken&UserCode=A2575&Function=CreateContact&PipelineId=3727019319364096972431828675722";
            var postData = new List>
            {
                
               };
            postData.Add(new KeyValuePair
            (
                   "Email[1]", (new KeyValuePair("Text", booking.Email),
                               new KeyValuePair("Type", "Work")).ToString()
               ));
    
            postData.Add(new KeyValuePair
               (
                   "Phone[1]", (new KeyValuePair("Text", booking.Phone),
                               new KeyValuePair("Type", "Work")).ToString()
               ));
    
            postData.Add(new KeyValuePair
               (
                   "Website[0]", (new KeyValuePair("Text", "")).ToString()
               ));
    
    
            postData.Add(new KeyValuePair
               (
                   "Location", (
                                new KeyValuePair("", booking.PostCode)
                                
                               ).ToString()
               ));
    
    
            postData.Add(new KeyValuePair
                (
    
                    "CustomFields", (
    
                        new KeyValuePair("Conservatory", booking.Consevatory),
                        new KeyValuePair("Size", booking.Size),
                        new KeyValuePair("Type", booking.RoofType),
                        new KeyValuePair("Source", booking.HearAboutUs),
                        new KeyValuePair("Comment", booking.OtherInfo),
                        new KeyValuePair("Conservatory", booking.SelectedProduct)
    
    
                    ).ToString()
    
    
                ));
        
    
            using (var httpClient = new HttpClient())
            {
                using (var content = new FormUrlEncodedContent(postData))
                {
                    content.Headers.Clear();
                    content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    
                    HttpResponseMessage response = await httpClient.PostAsync(url, content);
    
                    var apiresponse = await response.Content.ReadAsAsync();
                }
            }
            return true;
        }

And this is the LACRM PHP code:这是 LACRM PHP 代码:


    $_REQUEST['ContactName'],
        "Email"=>array(
                    0=>array(
                        "Text"=>"$_REQUEST[Email]",
                        "Type"=>"Work"
                    )
                ),
        "Phone"=>array(
                    0=>array(
                        "Text"=>"$_REQUEST[Phone]",
                        "Type"=>"Work"
                    )
                ),
    );
     
    //...And then use the "CallAPI" function to send the information to LACRM
    $Result = CallAPI($UserCode, $APIToken, $Function, $Parameters);
    //That's it, the contact will now show up in the CRM!
     
    //Now let's enter the "Comment" field from the HTML form as a note on the contact's record
    //Get the new ContactId
    $ContactId = $Result['ContactId'];
     
     
    $Function = "CreateNote";
    $Parameters = array(
        "ContactId"=>$ContactId,
        "Note"=>$_REQUEST['Comment']
    );
     
    //And pass that note to the API
    $Result = CallAPI($UserCode, $APIToken, $Function, $Parameters);
     
    /*
    There are all kinds of other things you might want to do here as well such as:
        -Set a task to follow up with the contact (https://www.lessannoyingcrm.com/help/topic/API_Example_Code_PHP/11/)
        -Add the contact as a lead (https://www.lessannoyingcrm.com/help/topic/API_Example_Code_PHP/87/)
        -Add the contact to a group (https://www.lessannoyingcrm.com/help/topic/API_Example_Code_PHP/13/)
        -Send an email to yourself letting you know a form was submitted (you can use the PHP "mail" function)
    */
     
     
    //Now forward the visitor to an html page confirming that we got their contact info
    header('location:confirm.html');
     
      
    /*
        This function takes all of the settings needed to call the API and puts them into
        one long URL, and then it makes an HTTP request to that URL.
    */
    function CallAPI($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 "Success!";
        }
        else{
            echo "API call failed. Error:".@$APIResult['Error'];
            exit;
        }
        return $APIResult;
    }

  

I would great appreciate any help in getting the customfield working.我将非常感谢任何帮助使自定义字段正常工作。 thank you谢谢你

In C#, the associative array is Dictionary .在 C# 中,关联数组是Dictionary You can prepare parameters like:您可以准备以下参数:

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
};

In the PHP example, the API call use the HTTP method GET, but you can use POST (and I advocate this).在 PHP 示例中,API 调用使用 HTTP 方法 GET,但您可以使用 POST(我提倡这样做)。 From the LACRAM documentation :从 LACRAM 文档

You can use POST, GET, or whatever else you want (it's all the same to us).您可以使用 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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM