function receivedMessage()

in SampleIntegrations/SampleBots/NodeJS/EmployeeSurvey/app.js [134:186]


function receivedMessage(event) {
	var senderID = event.sender.id;
	var recipientID = event.recipient.id;
	var timeOfMessage = event.timestamp;
	var message = event.message;

	console.log('Received message for user %d and page %d at %d with message:', 
		senderID, recipientID, timeOfMessage);
	console.log(JSON.stringify(message));

	var isEcho = message.is_echo;
	var messageId = message.mid;
	var appId = message.app_id;
	var metadata = message.metadata;

	// You may get a text or attachment but not both
	var quickReply = message.quick_reply;

	if (isEcho) {
		// Just logging message echoes to console
		console.log('Received echo for message %s and app %d with metadata %s', 
			messageId, appId, metadata);
		return;
	} else if (quickReply) {
		var quickReplyPayload = quickReply.payload;
		console.log('Quick reply for message %s with payload %s',
			messageId, quickReplyPayload);

		var payload_tokens = quickReplyPayload.split(':');
		var payload_action = payload_tokens[0];

		// We're using predefined metadata payloads for the quickreply messages
		// so let's use these to understand what should happen next
		switch (payload_action) {
			case 'DELAY_SURVEY':
				sendDelaySurvey(senderID);
				break;
			case 'START_SURVEY':
				sendFirstQuestion(senderID);
				break;
			case 'HAPPY':
				sendSecondQuestion(senderID);
				break;
			case 'STAY':
				sendThankYou(senderID);
				break;
			default:
				console.log('Quick reply tapped', senderID, quickReplyPayload);
				break;
		}
		return;
	}
}