Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Publicar mensajes en canal Telegram usando Python y un Bot

Tiempo de lectura: 3 minutos

Hola, hoy vamos a aprender cómo podemos publicar mensajes automáticamente usando un bot de Telegram usando Python.

Lo primero que tenemos que hacer es crear un bot de telegram, para ello vamos al BotFather https://telegram.me/BotFather

Ahora entramos en el chat y enviamos el comando:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/start
/start
/start

Ahora elegimos /newbot

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/newbot
/newbot
/newbot

Y nos solicita un nombre, introducimos el nombre de nuestro bot:

*Recuerda que el nombre tiene que tener el texto bot dentro del conjunto para que BotFather lo acepte.

Una vez añadido el nombre, nos devuelve el token de acceso que debemos apuntar.

Copiamos el token para tenerlo presente y vamos a crear un nuevo archivo Python.

Primero tenemos que instalar la libreria pyTelegramBotAPI

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install pyTelegramBotAPI
pip install pyTelegramBotAPI
pip install pyTelegramBotAPI

Ahora vamos a crear nuestro archivo mi_bot.py

Y añadimos el siguiente código:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import telebot
TOKEN_TELEGRAM = "token_copiado"
my_bot = telebot.TeleBot(TOKEN_TELEGRAM )
import telebot TOKEN_TELEGRAM = "token_copiado" my_bot = telebot.TeleBot(TOKEN_TELEGRAM )
import telebot
TOKEN_TELEGRAM = "token_copiado"
my_bot = telebot.TeleBot(TOKEN_TELEGRAM )

Con esto inicializamos el token.

Ahora tenemos que añadir nuestro Bot a un grupo o canal, para ello vamos al grupo o canal y lo buscamos para añadirlo, también le damos permisos de administrador.

Vamos a ajustes del canal > administradores:

Y elegimos añadir:

Y añadimos al bot con todos los permisos:

Ahora tenemos que obtener el id del chat para ello simplemente obtenemos el enlace del grupo o canal, en mi caso:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
https://t.me/quiero_libros_com
https://t.me/quiero_libros_com
https://t.me/quiero_libros_com

Y obtenemos la parte del final y añadimos @:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@quiero_libros_com
@quiero_libros_com
@quiero_libros_com  

Para publicar añadimos el siguiente codigo:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
CHAT_ID = "@quiero_libros_com "
my_bot.send_message(CHAT_ID, mensaje)
CHAT_ID = "@quiero_libros_com " my_bot.send_message(CHAT_ID, mensaje)
 CHAT_ID = "@quiero_libros_com  "
my_bot.send_message(CHAT_ID, mensaje)

El código final queda de esta forma:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import telebot
TOKEN_TELEGRAM = "token_copiado"
my_bot = telebot.TeleBot(TOKEN_TELEGRAM )
CHAT_ID = "@quiero_libros_com "
my_bot.send_message(CHAT_ID, mensaje)
import telebot TOKEN_TELEGRAM = "token_copiado" my_bot = telebot.TeleBot(TOKEN_TELEGRAM ) CHAT_ID = "@quiero_libros_com " my_bot.send_message(CHAT_ID, mensaje)
import telebot
TOKEN_TELEGRAM = "token_copiado"
my_bot = telebot.TeleBot(TOKEN_TELEGRAM )
CHAT_ID = "@quiero_libros_com  "
my_bot.send_message(CHAT_ID, mensaje)

Y este es el resultado:

Ahora ya podemos aplicar esto a nuestros chats o canales.

0

Deja un comentario