AI Engine Video Embedding

Want YouTube links sent by your chatbot to be embedded automatically? Look no further!

Installation

Add the following JavaScript to a post or page with an AI Engine chatbot on it:

(function waitForMwaiAPI(startTime) {
	if (typeof MwaiAPI !== 'undefined') {
		MwaiAPI.addFilter('ai.reply', function(reply) {
			const links = [];
			const regex = /https?:\/\/[^\s]+/g;
			let match;

			while ((match = regex.exec(reply)) !== null) {
				links.push(match[0]);
			}

			console.log('Parsed links:', links);
			const videoLinks = links.filter(link => link.includes('youtube.com'));
			console.log('Video links:', videoLinks);
			videoLinks.forEach(link => {
				const videoId = link.split('v=')[1].split('&')[0];
				const convoWidth = document.querySelector('.mwai-conversation').offsetWidth - 75;
				const aspectRatio = 16 / 9;
				const embedHtml = `<iframe width="${convoWidth}" height="${convoWidth / aspectRatio}" src="https://www.youtube.com/embed/${videoId}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`;
				reply = reply.replace(link, embedHtml);
			});
			return reply;
		});
	} else {
		if (Date.now() - startTime < 3000) {
			setTimeout(() => waitForMwaiAPI(startTime), 100);
		}
	}
})(Date.now());

Let’s enhance your business! I can help you develop custom AI engine extensions or broader AI strategies.
Contact me.