Source

plugins/youtube.js

  1. // Modules to install separately
  2. const request = require('request');
  3. const base64 = require('node-base64-image');
  4. const yts = require('yt-search');
  5. const youtubeUrl = 'https://youtube.com/watch/';
  6. function searchYoutube(search_){
  7. return new Promise(async (resolve, reject) => {
  8. const r = await yts( search_ );
  9. resolve(r.videos || []);
  10. })
  11. }
  12. const defaultConfig = {
  13. idChat: '',
  14. search: '',
  15. messageError: '*Ooops, an error occurred while trying to search, try again later*',
  16. messageNoDataFound: '*No information about your search could be found*'
  17. }
  18. /**
  19. * Plugin that allows you to search for videos directly from YouTube
  20. * @function youtube
  21. * @memberof Plugins
  22. * @param {string} idChat - Chat id to send the new image to
  23. * @param {string} search - Search parameter to perform
  24. * @param {string} messageError - Message to send in case of error
  25. * @param {string} messageNoDataFound - Message to send in case of not finding information
  26. */
  27. module.exports = {
  28. /**
  29. * Id - Name of the plugin to use
  30. * @property {string} id - Name of the plugin to use
  31. */
  32. id: 'youtube',
  33. plugin(_args) {
  34. const args = this.mergeOpts(defaultConfig, _args);
  35. if (args.idChat !== '' && args.search !== '') {
  36. searchYoutube(args.search.trim())
  37. .then(data => {
  38. if(data.length > 0){
  39. let videoYt = data[0];
  40. let options = {string: true}
  41. base64.encode(videoYt.thumbnail, options, (err, data64) => {
  42. if (err) {
  43. this.sendMessage({
  44. "idChat": args.idChat,
  45. "message": args.messageError
  46. });
  47. } else {
  48. this.sendLink({
  49. "idChat": args.idChat,
  50. "caption": "",
  51. "description": videoYt.description,
  52. "title": videoYt.title,
  53. "thumb": data64,
  54. "link": videoYt.url
  55. });
  56. }
  57. });
  58. }else {
  59. this.sendMessage({
  60. "idChat": args.idChat,
  61. "message": args.messageNoDataFound
  62. });
  63. }
  64. })
  65. }
  66. }
  67. };