-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbotsend.py
More file actions
101 lines (85 loc) · 3.28 KB
/
botsend.py
File metadata and controls
101 lines (85 loc) · 3.28 KB
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
#!/usr/bin/env python
import argparse
import os
from dotenv import load_dotenv
import telebot
import mimetypes
from telebot.apihelper import ApiTelegramException
import sys
load_dotenv()
API_KEY = os.getenv('API_KEY')
def last_chat_ids():
if not os.path.exists("last_admin_chat_ids"):
return []
with open("last_admin_chat_ids", "r") as f:
ids_str = f.read()
if not ids_str:
return []
return list(map(int, ids_str.split(',')))
CHAT_IDS = last_chat_ids()
if not API_KEY:
print("Falta API_KEY en el entorno.")
sys.exit(1)
bot = telebot.TeleBot(API_KEY)
parser = argparse.ArgumentParser(description="Enviar mensajes o archivos por Telegram desde línea de comandos.")
parser.add_argument('--message', help='Texto del mensaje a enviar. Si no se proporciona, se puede recibir desde stdin.')
parser.add_argument('--audio', help='Archivo de audio (ogg, mp3, wav)')
parser.add_argument('--image', help='Archivo de imagen (jpg, png, etc)')
parser.add_argument('--attach', help='Cualquier archivo para enviar como documento')
parser.add_argument('--voice', help='Nota de voz (ogg, wav, etc) enviada como mensaje de voz de Telegram')
parser.add_argument('--chat-id', help='Chat ID para enviar el mensaje. Si no se proporciona, se usarán todos los IDs de usuarios conectados.')
args = parser.parse_args()
# Determinar los chat IDs a usar
env_chat_id = os.getenv('TELEGRAM_BOT_CHAT_ID')
if args.chat_id:
target_chat_ids = [int(args.chat_id)]
elif env_chat_id:
target_chat_ids = [int(env_chat_id)]
else:
target_chat_ids = CHAT_IDS
# Envío de mensaje de texto
try:
if args.message:
for chat_id in target_chat_ids:
bot.send_message(chat_id, args.message)
elif not sys.stdin.isatty():
# Leer de stdin si hay datos disponibles
stdin_message = sys.stdin.read().strip()
if stdin_message:
for chat_id in target_chat_ids:
bot.send_message(chat_id, stdin_message)
# Envío de audio
if args.audio:
if not os.path.isfile(args.audio):
print(f"Archivo de audio no encontrado: {args.audio}")
sys.exit(1)
with open(args.audio, 'rb') as audio:
for chat_id in target_chat_ids:
bot.send_audio(chat_id, audio)
# Envío de imagen
if args.image:
if not os.path.isfile(args.image):
print(f"Imagen no encontrada: {args.image}")
sys.exit(1)
with open(args.image, 'rb') as img:
for chat_id in target_chat_ids:
bot.send_photo(chat_id, img)
# Envío de cualquier archivo
if args.attach:
if not os.path.isfile(args.attach):
print(f"Archivo no encontrado: {args.attach}")
sys.exit(1)
with open(args.attach, 'rb') as doc:
for chat_id in target_chat_ids:
bot.send_document(chat_id, doc)
# Envío de nota de voz (mensaje de voz)
if args.voice:
if not os.path.isfile(args.voice):
print(f"Archivo de voz no encontrado: {args.voice}")
sys.exit(1)
with open(args.voice, 'rb') as voice:
for chat_id in target_chat_ids:
bot.send_voice(chat_id, voice)
except ApiTelegramException as e:
print(f"Error al enviar mensaje: {e}")
sys.exit(1)