Demand background

In these two years, the competition of private domain operation is becoming increasingly fierce, and the enterprise wechat is not only the internal communication management tool of the company, but also the sharp tool used by the enterprise to precipitate the flow of private domain accurately to reach the user. So, how the enterprise WeChat customers associated with small procedures, the public, users must be a hardcore demand, imagine, if the user will WeChat the troika system completely through, each other diversion, accurate portraits depicting the user, so it can realize enterprise in the whole WeChat ecological sophisticated operation, build enterprise on the moat of WeChat system. Here is a detailed introduction of how to get through these 3.

Basic concepts literacy

The meaning of users: in enterprise wechat, there are actually two kinds of users, one is enterprise members: enterprise internal personnel, namely enterprise employees; The other is customers: outsiders brought in through business members. And customers are divided into two types: 1. Wechat customers; 2. Enterprise wechat customers. Need to be clear: we here to get through the enterprise micro channel with the public number of small program users, refers to the enterprise micro channel in the micro channel customers and the public number of small program associated.

UnionID: Understand the wechat system students should all know, as long as your public number, small program are bound to the same wechat open platform, so for the same wechat user UnionID is unique. Therefore, we can use UnionID to associate with a number of public small program users, which is also the key to get through with the enterprise wechat.

solution

We have mentioned above, in fact, to the public number, small program, enterprise wechat 3 through the key is through unionID association, as long as we bind the public number and small program to the same wechat open platform both of the unionID can get, now the problem is that the wechat open platform does not support binding enterprise wechat. Fortunately, the enterprise wechat can bind a public number or small program, after binding through the API provided by the enterprise wechat can obtain the wechat customer’s UnionID (with this customer in the binding public number or small program’s unionID), so that, You can use uninonID to associate the users of enterprise wechat, public number and small program.

steps

1. Background configuration

1. Open the function of adding customers for enterprise members

Enterprise Micro background operation path: Customer Contact > Permission Configuration > Scope

Enterprise members can add customers only after the contact function is enabled for them.

2. Enterprise wechat is bound to public accounts or small programs

Operation path of enterprise Wechat background: Customer contact > Customer > Click the “API” button > click the binding button next to “wechat Developer ID” to go to wechat public platform for authorization.

Note: The bound public account or small program subject should be consistent with the enterprise wechat main body, and only one can be bound. The authorized public account or small program should be bound to wechat open platform.

Call the interface to get data

After completing the background configuration, we can obtain the customer data with unionID by calling the API provided by enterprise wechat.

1. Obtain access_token

The access_token parameter must be provided in all enterprise wechat interface invocations. The access_token parameter must be obtained using the enterprise ID and secret. To obtain the enterprise ID, choose My Enterprise > Enterprise ID. Secret is special: Different business functions of enterprise wechat have different Secret. The secret of the customer contact function used here is the secret seen in the second step of the background configuration. Python pseudocode:

async def get_access_token() :
    params = {'corpid': 'xxx'.'corpsecret': 'xxx'}
    res = await get('https://qyapi.weixin.qq.com/cgi-bin/gettoken', params)
    access_token = res['access_token']
    return access_token
Copy the code

2. Get the list of members configured with the customer contact function

Python code implementation:

    async def get_staff_list(self) :
        """ Gets a list of members configured with customer contact. ""
        access_token = await self.get_token()
        url = f'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_follow_user_list?access_token={access_token}'
        res = await get(url, {})
        staff_list = res['follow_user']
        return staff_list
Copy the code

Returns the enterprise member ID, example: [“zhangsan”,”lisi”]

3. Obtain customer details in batches

Python code implementation:

    async def batch_get_customer_detail(self, userid, cursor) :
        "" Obtain customer details in bulk ""
        access_token = await self.get_token()
        url = f'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token={access_token}'
        body = {
            'userid': userid,
            'cursor': cursor,
            'limit': 100,
        }
        d = await post_json(url, {}, body)
        return d
Copy the code

If the enterprise member ID obtained in the previous step is passed into this method, the customer added through this enterprise member can be obtained. If the customer type is wechat customer, the returned information will contain the UnionID field.

At this point, we successfully associate the users of enterprise wechat, small program and public number through unionID, and achieve the user data through.

If you need a complete code, you can follow the public account “Frank’s Dream” and reply to “Enterprise wechat” for automatic access, or reply to “Add group” for group communication.

Advanced play

If you have customized development needs of enterprise wechat, you may be more interested in this part of the content. Above our implementation is to open on the level of user data, but you may not be satisfied with this, such as you want in your own little programs WeChat customer information directly, or even chat with our customer, so enterprise WeChat also provides special applet SDK, how can the following a brief introduction of small programs have the ability to do this.

1, small program associated with the enterprise wechat

Click on the official documentation

Enterprise micro background operation path: Application Management > Application > Create Application (Associated applets)

2, to configure the small program to use customer function permissions

Background operation path: Customer Contact > Customer > Open the “API” button > Callable Application > select the applets associated with the previous step

3. Call the interface of applets to achieve customization

After the above two steps, your applet will have the permission to call the applet SDK customer-related components provided by the enterprise wechat, such as popping up the customer selection list to initiate a session (Click to view the interface documentation).



Of course, the invocation of small program components also need to achieve enterprise wechat login and other processes, interested in canView official documentation.




Recommended reading:

  • Ngrok, a local development weapon. – Penetrates your Intranet​
  • What? One day to develop a mall small program? !​
  • How to make money from small program advertising?

PS: Welcome to my mall project, Github, Gitee