-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeep_bot.py
More file actions
70 lines (53 loc) · 1.92 KB
/
beep_bot.py
File metadata and controls
70 lines (53 loc) · 1.92 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
import telebot
import wave
import os
# Telegram bot token
BOT_TOKEN = "[Bot token]"
bot = telebot.TeleBot(BOT_TOKEN)
# Audio beep config
SAMPLE_RATE = 44100
BEEP_DURATION = 0.2 # seconds
# Frequencies for 1 and 0
FREQ_ONE = 1000
FREQ_ZERO = 500
# Output dir is current dir
output_dir = "."
def text_to_binary(text):
return ' '.join(format(ord(char), '08b') for char in text)
def generate_beep_wav(binary_str, filename):
print("Generating audio...")
import numpy as np
audio = []
for bit in binary_str.replace(" ", ""):
freq = FREQ_ONE if bit == '1' else FREQ_ZERO
samples = (np.sin(2 * np.pi * freq * t / SAMPLE_RATE)
for t in range(int(SAMPLE_RATE * BEEP_DURATION)))
audio.extend(samples)
audio = np.array(audio)
audio = (audio * 32767).astype(np.int16)
filepath = os.path.join(output_dir, filename)
with wave.open(filepath, 'w') as wav_file:
wav_file.setnchannels(1)
wav_file.setsampwidth(2)
wav_file.setframerate(SAMPLE_RATE)
wav_file.writeframes(audio.tobytes())
print(f"Saved audio to {filepath}")
return filepath
@bot.message_handler(func=lambda message: True)
def handle_message(message):
try:
text = message.text.strip()
print(f"\n[💬] Received: {text}")
binary = text_to_binary(text)
print(f"[🔢] Binary: {binary}")
filename = f"{message.from_user.id}_beep.wav"
wav_path = generate_beep_wav(binary, filename)
bot.send_message(message.chat.id, f"📡 Binary of your message:\n`{binary}`", parse_mode="Markdown")
bot.send_message(message.chat.id, f"Generating your audio.....")
bot.send_audio(message.chat.id, open(wav_path, 'rb'))
print("[✅] Audio sent.\n")
except Exception as e:
print(f"[❌] Error: {e}")
bot.send_message(message.chat.id, "Something went wrong!")
print("[🚀] Bot is running...")
bot.polling()