Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display dynamic reply in bubble chat from external API #52

Closed
User0123456789 opened this issue Apr 26, 2019 · 5 comments
Closed

Display dynamic reply in bubble chat from external API #52

User0123456789 opened this issue Apr 26, 2019 · 5 comments

Comments

@User0123456789
Copy link

I'm trying to display a dynamic message/reply message from external API, but I'm getting an error like this Uncaught TypeError: Cannot read property 'current' of undefined in askAgain function, but in alert it's working.

How to fix this? Also how to append a p html tag outside the bubble chat?

Thank you.

var original = false;
function askAgain(convState, ready, message) {
	convState.current.next = convState.newState({
		type: 'text',
		questions: [message],
		callback: ['clientConcern']
	});
	convState.current.next.next = convState.newState({
		type: 'text',
		questions: [message],
		text: $('input[type=text]#chatConcern').val()
	});

	setTimeout(ready, Math.random() * 500 + 100);
	convState.current.next = false;
}


function clientConcern(convState){
	$.ajax({
		url: 'http://example/api',
		type: "POST",
		dataType: "json",
		contentType: 'application/json; charset=utf-8',
		data: JSON.stringify({
			"nickname": $('input[type=text]#custName').val(),
			"subject": $('select[name="custType"]').val()
		}),
		success: function (data){
			loadInterval = setInterval(receivedMsg, 1000);

		// load messages uniquely
		var messageInterval = setInterval(function(){
			for (const item of messageArray){
				if (!showedMap.has(item.index)){
					askAgain(item.text);
					showedMap.set(item.index, true);
				}
			}
		}, 100);
						
		sendMsg();
	},
		error: function (data){
			alert(data);
		}
	});
}
        
function sendMsg(){
	jQuery.ajax({
		url: 'http://example/api/chat_id',
		type: "POST",
		dataType: "json",
		contentType: 'application/json; charset=utf-8',
		data: JSON.stringify({
			operationName : "SendMessage",
			alias : ALIAS,
			secureKey : SECURE_KEY,
			text: $('input[type=text]#chatConcern').val()
		}),
		success: function (data){
			console.log(data);
		}
	});
}

function receivedMsg(convState){
	jQuery.ajax({
		method: "POST",
		url: "http://example/api/chat_id/messages",
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		data: JSON.stringify({
			"alias" : ALIAS,
			"secureKey" : SECURE_KEY,
			"userId" : USER_ID
		}),
	}).done(function(data){
		if (data.chatEnded){
			clearInterval(loadInterval);
			original = convState.current;
			convState.current.next = convState.newState({
				type: 'text',
				questions: [message],
				noAnswer: true
			});
			convState.current.next.next = convState.newState({
				type: 'text',
				questions: ['Chat ended.'],
				noAnswer: true,
				callback: ['askAgain']
			});
		} else {
			var len = data.messages.length;
			for (i = 0; i < len; i++){
				var messages = data.messages[i];

				if (messages.type === "Message" && 
				(messages.from.type === "Agent" || messages.from.type === "External")){
					if (!messageMap.has(messages.index)){
						messageMap.set(messages.index, true);
						messageArray.push(messages);
					}
				}
			}
		}	
	});
}

jQuery(function($){
	var convForm = $('#chat').convform();
	console.log(convForm);
});
```
@eduardotkoller
Copy link
Owner

eduardotkoller commented Apr 26, 2019 via email

@User0123456789
Copy link
Author

User0123456789 commented Apr 27, 2019 via email

@User0123456789
Copy link
Author

User0123456789 commented Apr 28, 2019

Or there's a way that I can get the response from an external API and display it dynamically to bubble chat? I tried to use append to display the response from API, but it's not working. I mean the response didn't display.

@turbocpt
Copy link

Did you manage to find a solution to this issue?

@eduardotkoller
Copy link
Owner

To anyone reading this, see if issue #62 can help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants