PageRenderTime 69ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/services/duckduckgo.js

https://bitbucket.org/slimdll/botphp
JavaScript | 58 lines | 56 code | 2 blank | 0 comment | 4 complexity | e2d9f334e51424827235e004880bbfe1 MD5 | raw file
  1. 'use strict'
  2. const s = require('../settings')
  3. const url = require('url')
  4. const http = require('http')
  5. const parse = { 'parse_mode': 'HTML' }
  6. const stickers = [
  7. 'BQADBAADMgEAAl6A9AWiXNcdh4N2fgI',
  8. 'BQADBAADzQADXoD0BfaPN-SRlpBYAg',
  9. 'BQADBAADxQADXoD0Be6MWaqIBanrAg',
  10. 'BQADBAADAQEAAl6A9AVrEFjvEfTbRwI',
  11. 'BQADBAADOQEAAl6A9AWLW7oQoiHXdAI',
  12. 'BQADBAADBwEAAl6A9AXuD8xAc5avLwI',
  13. 'BQADBAADxwADXoD0BaTJK9_y3lrtAg',
  14. 'BQADBAADyQADXoD0BYyFKrC9hFpcAg',
  15. 'BQADBAADywADXoD0BaJ-5YWTuZxTAg',
  16. 'BQADBAADzwADXoD0BactihrL_9LKAg',
  17. 'BQADBAAD6wADXoD0Bbi4Fg2kp0fUAg'
  18. ]
  19. const execute = (bot, msg, match) => {
  20. const query = match.query
  21. const _base = 'http://api.duckduckgo.com/?format=json&q='
  22. const _url = url.parse(_base + encodeURIComponent(query))
  23. _url.headers = {
  24. 'User-Agent': 'Telegram Bot',
  25. 'Accept-Language': 'pt-BR;q=1, pt;q=0.8, en;q=0.5'
  26. }
  27. const req = http.request(_url, (res) => {
  28. let data = ''
  29. res.on('data', (chunk) => data += chunk)
  30. res.on('end', (err) => {
  31. try {
  32. data = JSON.parse(data)
  33. if (data.AbstractText !== '') {
  34. const _return = 'Achei isso no DuckDuckGo: <i>' + data.AbstractText + '</i> Saiba mais em ' + data.AbstractURL
  35. bot.sendMessage(msg.chat.id, _return, parse).catch(console.log)
  36. console.log('data): ' + data)
  37. } else {
  38. const sticker = stickers[Math.floor(Math.random() * stickers.length)]
  39. const _return = '<b>Não achei nada jovem!</b> Sorry mesmo, mas você pode fumar um pra relaxar.'
  40. bot.sendMessage(msg.chat.id, _return, parse).catch(console.log)
  41. s.get(msg.chat.id, 'stickers', (err, data) => {
  42. if (data == 'true') bot.sendSticker(msg.chat.id, sticker).catch(console.log)
  43. })
  44. }
  45. } catch (e) {
  46. bot.sendMessage(msg.chat.id, 'DEU MERDA: ' + e).catch(console.log)
  47. console.log('Erro end: ' + err)
  48. }
  49. })
  50. })
  51. req.end()
  52. req.on('error', (e) => console.error(e))
  53. }
  54. module.exports = {
  55. execute
  56. }