def chat_view(request): dialogflow = Dialogflow(**settings.DIALOGFLOW) input_dict = convert(request.body) input_text = json.loads(input_dict)['text'] responses = dialogflow.text_request(str(input_text)) if request.method == 'GET': data = { 'detail': 'You should make a POST request to this endpoint', 'name': '/chat' } return JsonResponse(data, status=405) elif request.method == 'POST': data = { 'text': responses[0], } return JsonResponse(data, status=200) elif request.method == "PATCH": data = { 'detail': 'You should make a POST request to this endpoint.', 'name': '/chat' } return JsonResponse(data, status=405) elif request.method == "DELETE": data = { 'detail': 'You should make a POST request to this endpoint.', 'name': '/chat' } return JsonResponse(data, status=405)
def chat_view(request): dialogflow = Dialogflow(**settings.DIALOGFLOW) input_dict = convert(request.body) input_text = json.loads(input_dict)['text'] responses = dialogflow.text_request(str(input_text)) if request.method == "GET": # Return a method not allowed response data = { 'detail': 'You should make a POST request to this endpoint.', 'name': '/chat' } return JsonResponse(data, status=405) elif request.method == "POST": data = { 'text': responses[0], } print(str(input_text)) out_str = '' MedicinesModel = Medicines.objects.all() for medicine in MedicinesModel: if str(input_text).find(medicine.name) != -1: print(medicine.name) out_str += 'Medicine Information --> ' + medicine.name + ' :: ' if medicine.alternative_medicines is not None: out_str += 'Alternative Medicines --> ' + medicine.alternative_medicines + ' :: ' out_str += 'Medicine Indication --> ' + medicine.indication AllStores = Stores.objects.all() out_str += ':: Medicine are available at store --> ' for store in AllStores: #print('store available'+store.medicines_available) if str(medicine.name) in store.medicines_available: out_str += 'Store Name: ' + store.name + ' Address: ' + store.address print('store available') data = { 'text': out_str, } return JsonResponse(data, status=200) elif request.method == "PATCH": data = { 'detail': 'You should make a POST request to this endpoint.', 'name': '/chat' } # Return a method not allowed response return JsonResponse(data, status=405) elif request.method == "DELETE": data = { 'detail': 'You should make a POST request to this endpoint.', 'name': '/chat' } # Return a method not allowed response return JsonResponse(data, status=405)
def chat(request): if request.method == 'POST': form = ChatForm(request.POST) if form.is_valid(): mess = form.save(commit=False) user_uid = Profile.objects.get(user=request.user) mess.user_uuid = user_uid dialogflow = Dialogflow(**settings.DIALOGFLOW) responses = dialogflow.text_request(str(mess.message)) mess.response = responses[0] if(len(mess.response.split())!=2): mess.save() return render(request, 'chat.html', {'mess': mess}) else: source, destination = mess.response.split() source_code_response = requests.get('https://api.railwayapi.com/v2/name-to-code/station/'+source+'/apikey/rfgbncxndq/') dest_code_response = requests.get('https://api.railwayapi.com/v2/name-to-code/station/'+destination+'/apikey/rfgbncxndq/') source_code_json = source_code_response.json() dest_code_json = dest_code_response.json() source_code = source_code_json["stations"][0]["code"] dest_code = dest_code_json["stations"][0]["code"] final_response = requests.get('https://api.railwayapi.com/v2/between/source/'+source_code+'/dest/'+dest_code+'/date/<15-09-2018>/apikey/rfgbncxndq/') final_response_json = final_response.json() final_response_string = json.dumps(final_response_json) mess.response = final_response_string mess.save() return render(request, 'chat.html', {'mess': mess}) else: form = ChatForm() return render(request, 'sendMessage.html', {'form': form})
#!/usr/bin/env python from dialogflow_lite.dialogflow import Dialogflow # demo agent access token: e5dc21cab6df451c866bf5efacb40178 client_access_token = 'e5dc21cab6df451c866bf5efacb40178' dialogflow = Dialogflow(client_access_token=client_access_token) response = dialogflow.text_request('how are you') print(response) response = dialogflow.text_request('howz the weather today') print(response)