1

I have this project where I have to connect my Django app with a Chatfuel bot.

I have my admin panel, so whenever I update a field like complete certain task, I have to notify my client through the chatbot that this field change. I read JSON API docs and I noticed that they have an specific "template" to get data from a backend.

What I did is extract all my data from the models through Django Rest Framework and convert it to a JSON. The thing is I don't know how to use this information to work with it in Chatfuel because my JSON hasn't the template that Chatfuel requires.

This is my info extracted from the models.

Here's my info extracted from my models

This is what Chatfuel needs. Here's what Chatfuel need.

Pilar Figueroa
  • 85
  • 1
  • 11
  • Your question is to broad. Who is responsible to detect the changes within your models? Do you have a separate program that reads the django API and feeds it to chatfuel, or do you want django to do this when something changes? – dahrens Jan 14 '18 at 23:30
  • @dahrens I want django do this when something changes. – Pilar Figueroa Jan 14 '18 at 23:32
  • Do you only need to write messages when the model you've shown changes? Can you please add code of the model and the serializer class? – dahrens Jan 15 '18 at 00:37

2 Answers2

0

If someone is looking for this answer, I solve it with a Serializer:

class UserAPI(APIView):
serializer = UserSerializer


def get(self, request, format=None):
    list = Caseworker.objects.all()
    response = self.serializer(list, many=True)

    return HttpResponse(json.dumps({'messages': {'text': 
    response.data}}), content_type='application/json')
Pilar Figueroa
  • 85
  • 1
  • 11
0

You can achieve this with Runkit using express (node).

1) Go to https://runkit.com and create a notebook with this code and replace: ThisIsYourJsonCall with your call:

var express = require('express');
var app = express();
var getJSON = require("async-get-json");

app.listen(80, function() {
    console.log('Chatfuel Bot-Server listening on port 80...');
});

app.get('/*', function(req, res) {
  module.exports.endpoint = async function (request, response)
    {
    var stringResponse = JSON.stringify(await getJSON('ThisIsYourJsonCall'));
    response.end(stringResponse); 
    }
});

2) Publish the snippet and click the endpoint link, where you can see the response. Grab the url which ends with '.sh' and put it in Json plugin in chatfuel.

3) What you need to do now is to change the stringResponse variable to the format you and Facebook needs using javascript and node before you pass it to the response.end() function.