intercom-php API - creating conversation using an email returns 400 bad request

554 Views Asked by At

intercom-php version: 4.2

Intercom API version: 1.4

PHP version: 7.3

Expected behavior: I am trying (as per the documentation here: https://developers.intercom.com/intercom-api-reference/v1.4/reference#user-or-contact-initiated-conversation) to initiate a conversation using an email address, like so:

$response = $client->messages->create([
    'message_type' => 'inapp',
    'body' => 'Hello....',
    'from' => [
        'type' => 'contact',
        'email' => '[email protected]',
    ]
]);

I am expecting this to create the conversation, as the documentation states:

"The sending user or lead is identified by their user_id, email or id values in the from field, along with a type field value of user or contact."

Actual behavior: This returns a 400 bad request response, if 'email' is used in the from field.

Logs: PHP Fatal error: Uncaught Http\Client\Common\Exception\ClientErrorException: Bad Request in .../php-http/client-common/src/Plugin/ErrorPlugin.php:7

1

There are 1 best solutions below

0
On

I believe this is happening due to

  • multiple records matching the same email
  • and the PHP library not showing the full error message

If multiple records match, you will need to use user_id / id instead of email. It should match an existing unique record for it to work

Check for multiple records matching

  • Log into your Intercom app and search for the email in question
  • contact in the API corresponds to Leads in the web interface multiple records with the same email

View raw error messages

$ curl https://api.intercom.io/messages \
-XPOST \
-H 'Authorization:Bearer YOUR_INTERCOM_ACCESS_TOKEN' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' -d'
{
  "from": {
    "type": "contact",
    "email": "[email protected]"
  },
  "body": "Hey"
}'
{"type":"error.list","request_id":"003858akvkngsbrdj3h0","errors":[{"code":"multiple_users","message":"Multiple contact profiles found with the provided email. Please be more specific using user_id."}]}

Finding id / user_id value

  • Click into the record in the web interface
  • id should be in the URL https://app.intercom.com/a/apps/your_app_id/users/LONG_STRING_WHICH_IS_THE_ID/all-conversations the format should look something like this 5fe8c9742114721c54d22754
  • user_id should be the "User ID" attribute value

Finding id / user_id value