RandomTube
@lucasew
code:
data:privatelast updated:1 year ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
nodejs
auth
to use OAuth tokens and API keys in code via theauths object
(auths.telegram_bot_api)
(auths.youtube_data_api)
params
Authorized chat
 
integer ·params.authorized_chat
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
}
136
const axios = require('axios');
const authorized_chat = params.authorized_chat
const {host} = steps.trigger.event.headers
const url = new URL(event.url)
async function justReturn() {
  return await $respond({
    status: 200,
    body: "ok"
  })
}
const that = this;
if (!$checkpoint) {
  $checkpoint = {}
}
if (!$checkpoint.videos) {
  $checkpoint.videos = []
}
async function invokeTelegram(method, data) {
  return await axios({
    method: "GET",
    url: `https://api.telegram.org/bot${auths.telegram_bot_api.token}/${method}`,
    data
  })
}
const handler = {
  async deleteIds() {
    const isTelegramAllowed = !event.query.disallowTelegram
    console.log(isTelegramAllowed)
    const isTelegram = event.headers["user-agent"].indexOf("Telegram") !== -1

    if ((isTelegram && isTelegramAllowed) || !isTelegram) {
      const bodyFileIds = event.body.files !== undefined 
      ? event.body.files.length > 0 ? event.body.files : []
      : [];
      const queryFileIds = event.query.ids != undefined
      ? event.query.ids.split(',')
      : [];
      const fileIds = [...bodyFileIds, ...queryFileIds]
      $checkpoint.videos = $checkpoint.videos.filter((video) => {
        return fileIds.indexOf(video.file_id) === -1
      })
      console.log(fileIds)
    } else {
      console.log("Telegram requested and not allowed")
    }
    await justReturn()
  },
  async notify() {
    await invokeTelegram("sendMessage", {
      text: event.body.messsage || `mensagem vazia enviada por ${event.client_ip}`,
      chat_id: authorized_chat
    })
    return justReturn()
  },
  async yta() {
    return await $respond({
      immediate: true,
      status: 200,
      body: auths.youtube_data_api
    })
  },
  async telegram() {
    async function pushVideo({file_id, duration, file_size}) {
      if (file_size > (20*1024*1024)) {
        return await reply(`erro: ${file_size} bytes é mais que 20MB, não vou conseguir baixar`)
      }
      $checkpoint.videos.push({
        file_id: file_id,
        length: duration
      })
      await reply(`tá na lista, patrão\nremover: ${host}/deleteIds?ids=${file_id}&disallowTelegram=1`)
    }
    async function reply(text, parse_mode) {
      await invokeTelegram("sendMessage", {
        text, 
        chat_id: event.body.message.chat.id,
        reply_to_message_id: event.body.message.message_id,
        parse_mode,
      })
    }
    if (event.body.message?.chat?.id !== authorized_chat) {
      await justReturn()
      return await invokeTelegram("sendMessage", {
        text: `Usuário @${event.body.message?.from?.username} tentou usar o bot sem autorização pelo chat ${event.body.message?.chat?.username}`,
        chat_id: authorized_chat
      })
    }
    const {video, animation, message_id, document} = event.body.message
    if (video) {
      await pushVideo(video)
    } else if (animation) {
      await pushVideo(animation)
    } else if (document && document.mime_type === "video/mp4") {
      await reply("quase lá, manda para o @convert2filebot que ele resolve esse arquivo")
    } else {
      await reply("erro: não é vídeo nem gif")
    }    
    return await justReturn()
  },
  async list() {
    return await $respond({
      immediate: true,
      status: 200,
      body: {
        ...$checkpoint,
        reportChatID: params.authorized_chat
      }
    })
  },
  async len() {
    let secs = 0
    const videos = $checkpoint.videos
    let qt = videos.length
    for (let i = 0; i < qt; i++) {
      secs += videos[i].length || 0
    }
    return await $respond({
      immediate: true,
      status: 200,
      body: {
        secs,
        qt
      }
    })
  },
  "favicon.ico": async function() {
    return await $respond({
      immediate: true,
      status: 404
    })
  }
}[url.pathname.slice(1)]

handler && await handler()