Removing Contract Addresses in Telegram

Is there any alternative way to every upcoming contract addresses in my Telegram Community like RemoveETHAddress bot? Please see the code from below. It won’t let me use ‘getUpdates’ unless I delete the webhook, if I delete the webhoo the bot will no longer monitor the server

import { axios } from "@pipedream/platform";

export default defineComponent({
  props: {
    telegramBotToken: {
      type: "string",
      label: "Telegram Bot Token",
      secret: true,
      description: "Your Telegram bot token",
    },
    chatId: {
      type: "string",
      label: "Chat ID",
      description: "The ID of the Telegram chat where you want to monitor contract addresses",
    },
  },
  async run({ $ }) {
    const deleteWebhookUrl = `https://api.telegram.org/bot${this.telegramBotToken}/deleteWebhook`;

    await axios($, {
      method: "GET",
      url: deleteWebhookUrl,
    });

    const regex = /0x[a-fA-F0-9]{40}/;
    const getUpdatesUrl = `https://api.telegram.org/bot${this.telegramBotToken}/getUpdates`;

    const updates = await axios($, {
      method: "GET",
      url: getUpdatesUrl,
    });

    for (const update of updates.result) {
      if (update.message.chat.id === this.chatId && regex.test(update.message.text)) {
        const warningMessage = "Warning: A contract address has been detected in the chat.";
        const sendMessageUrl = `https://api.telegram.org/bot${this.telegramBotToken}/sendMessage`;

        await axios($, {
          method: "POST",
          url: sendMessageUrl,
          data: {
            chat_id: this.chatId,
            text: warningMessage,
          },
        });
      }
    }
  },
});

Hi @joseph.jumilla,

You can use Pipedream to monitor your Telegram chat for contract addresses using a different approach. Instead of using the getUpdates method, you can set up a webhook to receive updates from Telegram directly in a Pipedream workflow. Here’s how you can do it:

  1. Create a new Pipedream workflow with an HTTP trigger.
  2. Copy the endpoint URL of the HTTP trigger.
  3. Set up the webhook in Telegram by making a request to https://api.telegram.org/bot<your_telegram_bot_token>/setWebhook?url=<pipedream_http_trigger_url>.
  4. In the Pipedream workflow, add a new Node.js code step and use the following code:
const regex = /0x[a-fA-F0-9]{40}/;

if (steps.trigger.event.body.message.chat.id === this.chatId && regex.test(steps.trigger.event.body.message.text)) {
  const warningMessage = "Warning: A contract address has been detected in the chat.";
  const sendMessageUrl = `https://api.telegram.org/bot${this.telegramBotToken}/sendMessage`;

  await axios($, {
    method: "POST",
    url: sendMessageUrl,
    data: {
      chat_id: this.chatId,
      text: warningMessage,
    },
  });
}

This code will check for contract addresses in the incoming messages and send a warning message if a contract address is detected. Make sure to add the telegramBotToken and chatId props to the component as in your original code.

With this approach, you don’t need to delete the webhook, and your bot will continue monitoring the chat.
I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double check my answers and code! Learn how to build your own.

You can either use long polling or webhook, not both.

1 Like

Hi I already fixed it, the AI helped a lot with the process

1 Like