Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hawkz/b5cfc65ec61ddb225ea6695f82341976 to your computer and use it in GitHub Desktop.
Save hawkz/b5cfc65ec61ddb225ea6695f82341976 to your computer and use it in GitHub Desktop.
# Django & Intercom.io
Create a template tag:
```python
import hashlib as h
import hmac as h2
@register.filter(name='hmac')
def hmac(userid, key):
"""Return the encrypted key"""
return h2.new(str(key), str(userid), digestmod=h.sha256).hexdigest()
```
In your template do something like:
```django
{% load your_template_tag_here %}
<script>
{% if not user.is_authenticated %}
window.intercomSettings = {
app_id: "XXXXXXXX"
};
{% else %}
window.intercomSettings = {
app_id: "XXXXXXXX",
name: "{{ request.user.get_full_name }}", // Full name
user_id: "{{ request.user.id }}", // User ID
created_at: {{ request.user.date_joined|date:"U" }}, // Signup date as a Unix timestamp
user_hash: "{{ request.user.id|hmac:'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' }}"
};
{% endif %}
</script>
```
@hawkz
Copy link
Author

hawkz commented Aug 12, 2016

Django / Intercom.io

Create a template tag:

import hashlib as h 
import hmac as h2

@register.filter(name='hmac')
def hmac(userid, key): 
    """Return the encrypted key"""
    return h2.new(str(key), str(userid), digestmod=h.sha256).hexdigest()

In your template do something like:

{% load your_template_tag_here %}

<script> 
{% if not user.is_authenticated %} 
    window.intercomSettings = { 
      app_id: "XXXXXXXX" 
    }; 
{% else %} 
    window.intercomSettings = { 
      app_id: "XXXXXXXX", 
      name: "{{ request.user.get_full_name }}", // Full name 
      user_id: "{{ request.user.id }}", // User ID 
      created_at: {{ request.user.date_joined|date:"U" }}, // Signup date as a Unix timestamp 
      user_hash: "{{ request.user.id|hmac:'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' }}" 
    }; 
{% endif %} 
</script> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment