Overview and Prerequisites
- Ensure you have accounts and proper credentials for both Microsoft Azure and Intercom.
- Make sure you have access to Azure Cognitive Services with necessary API keys.
- Intercom developer access is required to work with their API integrations.
Set up Azure Cognitive Services
- Navigate to the Azure Portal and select 'Create a resource'.
- Search for 'Cognitive Services' and create it, selecting appropriate settings and resource group.
- Once deployed, access the resource and note the keys and endpoint needed for API calls.
Build a Middleware in Your Application
- Create a new file in your application for handling interactions between Intercom and Azure.
- Set up an HTTP endpoint that listens to incoming messages from Intercom via webhook.
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
@app.route('/intercom-webhook', methods=['POST'])
def intercom_webhook():
data = request.json
message = data['data']['item']['conversation_parts']['conversation_parts'][0]['body']
response = call_cognitive_service(message)
return jsonify({"response": response})
def call_cognitive_service(message):
# Add logic to call Azure Cognitive Service
pass
if __name__ == "__main__":
app.run(port=5000)
Implement Azure Cognitive Services
- Use the endpoint and API key from your Azure Cognitive Service instance to authenticate and call the service.
- Choose the appropriate service given your need, e.g., Text Analytics, Translator, etc.
def call_cognitive_service(message):
subscription_key = "your_azure_subscription_key"
endpoint = "your_azure_endpoint"
headers = {
'Ocp-Apim-Subscription-Key': subscription_key,
'Content-Type': 'application/json'
}
body = {
"documents": [{"id": "1", "text": message}]
}
response = requests.post(endpoint, headers=headers, json=body)
return response.json()
Set Up Intercom Webhook
- Go to your Intercom settings and navigate to the 'Webhooks' section.
- Create a new webhook subscription. Set the webhook URL to the endpoint you created in the middleware.
- Select the appropriate events, such as new conversation part, to trigger the webhook.
Handle Responses and Actions
- Once you receive a response from Azure Cognitive Services, use Intercom API to post a reply to the conversation.
def reply_to_intercom(conversation_id, response_text):
token = "your_intercom_access_token"
url = f"https://api.intercom.io/conversations/{conversation_id}/reply"
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
data = {
"message_type": "comment",
"body": response_text
}
requests.post(url, headers=headers, json=data)
Test and Debug
- Send test messages from Intercom to ensure that they are correctly processed by Azure and responded to appropriately.
- Check logs in your application to debug any issues or errors that occur.
Deploy Your Middleware
- Deploy the middleware service on a cloud platform like Heroku, AWS, or Azure itself for production use.
- Ensure that the endpoint is securely accessible and rate limits or scaling issues are accounted for.
git push heroku main