Hey there, fellow Go enthusiast! Ready to dive into the world of Bitrix24 CRM API integration? Buckle up, because we're about to embark on a journey that'll have you wielding the power of go-bitrix24 like a pro. Let's get cracking!
Bitrix24 CRM is a powerhouse when it comes to customer relationship management, and its API is the key to unlocking its full potential. We'll be using the go-bitrix24 package to make our lives easier and our code cleaner. Trust me, you're gonna love it!
Before we jump in, make sure you've got:
Let's kick things off by creating a new Go project:
mkdir bitrix24-integration cd bitrix24-integration go mod init bitrix24-integration
Now, let's bring in our secret weapon - the go-bitrix24 package:
go get github.com/go-bitrix24/go-bitrix24
Time to get our hands dirty! First, let's import the necessary packages and create a new Bitrix24 client:
package main import ( "fmt" "github.com/go-bitrix24/go-bitrix24" ) func main() { client, err := bitrix24.NewClient("https://your-domain.bitrix24.com", "your_access_token") if err != nil { panic(err) } fmt.Println("Bitrix24 client initialized successfully!") }
Now that we're connected, let's flex those API muscles!
Want to grab some leads? It's as easy as pie:
leads, err := client.CRM.Lead.List(bitrix24.ListParams{}) if err != nil { panic(err) } fmt.Printf("Found %d leads\n", len(leads))
Let's add a new contact to the mix:
newContact := bitrix24.Contact{ Name: "John Doe", Email: []bitrix24.ContactEmail{{Value: "[email protected]", ValueType: "WORK"}}, } contactID, err := client.CRM.Contact.Add(newContact) if err != nil { panic(err) } fmt.Printf("Created new contact with ID: %d\n", contactID)
Oops, John got married! Let's update his last name:
updateFields := map[string]interface{}{ "LAST_NAME": "Smith", } updated, err := client.CRM.Contact.Update(contactID, updateFields) if err != nil { panic(err) } fmt.Printf("Contact updated: %v\n", updated)
Say goodbye to that old lead:
deleted, err := client.CRM.Lead.Delete(oldLeadID) if err != nil { panic(err) } fmt.Printf("Lead deleted: %v\n", deleted)
Ready to level up? Let's explore some advanced techniques!
Need to update a bunch of deals at once? Batch operations have got your back:
batch := client.NewBatch() batch.Add("crm.deal.update", map[string]interface{}{"id": 1, "fields": map[string]interface{}{"TITLE": "Updated Deal 1"}}) batch.Add("crm.deal.update", map[string]interface{}{"id": 2, "fields": map[string]interface{}{"TITLE": "Updated Deal 2"}}) results, err := batch.Exec() if err != nil { panic(err) } fmt.Printf("Batch results: %v\n", results)
Don't let network hiccups bring you down. Implement some retry logic:
maxRetries := 3 for i := 0; i < maxRetries; i++ { _, err := client.CRM.Lead.Get(leadID) if err == nil { break } if i == maxRetries-1 { fmt.Printf("Failed after %d retries: %v\n", maxRetries, err) } time.Sleep(time.Second * 2) }
Let's put it all together and sync some leads with a local database:
func syncLeads(client *bitrix24.Client, db *sql.DB) error { leads, err := client.CRM.Lead.List(bitrix24.ListParams{}) if err != nil { return err } for _, lead := range leads { // Check if lead exists in local DB var localID int err := db.QueryRow("SELECT id FROM leads WHERE bitrix_id = ?", lead.ID).Scan(&localID) if err == sql.ErrNoRows { // Insert new lead _, err = db.Exec("INSERT INTO leads (bitrix_id, title, status) VALUES (?, ?, ?)", lead.ID, lead.Title, lead.Status) } else if err == nil { // Update existing lead _, err = db.Exec("UPDATE leads SET title = ?, status = ? WHERE id = ?", lead.Title, lead.Status, localID) } if err != nil { return err } } return nil }
Don't forget to test your integration! Here's a quick unit test to get you started:
func TestLeadCreation(t *testing.T) { client, _ := bitrix24.NewClient("https://your-domain.bitrix24.com", "your_access_token") newLead := bitrix24.Lead{Title: "Test Lead"} leadID, err := client.CRM.Lead.Add(newLead) if err != nil { t.Fatalf("Failed to create lead: %v", err) } if leadID == 0 { t.Fatalf("Expected non-zero lead ID") } }
To keep your integration running smoothly:
And there you have it, folks! You're now armed with the knowledge to build a robust Bitrix24 CRM API integration in Go. Remember, practice makes perfect, so don't be afraid to experiment and push the boundaries of what you can do with this powerful combination.
For more in-depth information, check out the go-bitrix24 documentation and the official Bitrix24 API docs. Now go forth and code something awesome!