-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverintegration1.py
More file actions
147 lines (117 loc) Β· 4.65 KB
/
Copy pathserverintegration1.py
File metadata and controls
147 lines (117 loc) Β· 4.65 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
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
137
138
139
140
141
142
143
144
145
146
147
from flask import Flask, jsonify
from telegram import Bot, Update
from telegram.ext import Updater, MessageHandler, Filters, CallbackContext
from pydub import AudioSegment
from deepgram import Deepgram
import os
import string
from threading import Thread
# ============== Configuration ==============
TELEGRAM_TOKEN = "__"
DEEPGRAM_API_KEY = "__"
app = Flask(__name__)
bot = Bot(token=TELEGRAM_TOKEN)
deepgram = Deepgram(DEEPGRAM_API_KEY)
latest_command = ""
# ============== Flask Endpoint ==============
@app.route("/get_command", methods=["GET"])
def get_command():
global latest_command
command = latest_command.rstrip(string.punctuation).strip()
print(f"π° Sending to ESP32: {command}")
latest_command = "" # Clear after sending
return jsonify({"command": command})
# ============== Deepgram Helper Function ==============
def transcribe_with_deepgram(wav_path):
try:
with open(wav_path, "rb") as audio:
source = {"buffer": audio, "mimetype": "audio/wav"}
response = deepgram.transcription.sync_prerecorded(source, {"punctuate": True})
transcript = response['results']['channels'][0]['alternatives'][0].get('transcript', '')
return transcript
except Exception as e:
print("π¨ Deepgram transcription error:", e)
return ""
# ============== Command Cleaner ==============
def clean_command(text):
# Remove trailing punctuation from the transcription (e.g., "Walk." -> "Walk")
text = text.strip().rstrip(string.punctuation)
# Step 1: Join adjacent single-character words (e.g., "P R" -> "PR")
words = text.split()
result = []
buffer = ""
for word in words:
if len(word) == 1:
buffer += word
else:
if buffer:
result.append(buffer)
buffer = ""
result.append(word)
if buffer:
result.append(buffer)
cleaned = ' '.join(result)
# Step 2: Manual mode replacements
replacements = {
"Forward": "PF",
"Backward": "PB",
"Right": "PR",
"Left": "PL",
"Up": "PU",
"Down": "PD",
"Rotate right": "PRR",
"Rotate left": "PRL",
"Stop": "R"
}
for key, value in replacements.items():
if cleaned.lower() == key.lower():
print(f"π Replaced '{key}' with '{value}'")
return value
return cleaned
# ============== Telegram Handlers ==============
def handle_voice(update: Update, context: CallbackContext):
global latest_command
user_id = update.message.from_user.id
file = bot.getFile(update.message.voice.file_id)
ogg_path = f"voice_{user_id}.ogg"
wav_path = f"voice_{user_id}.wav"
try:
file.download(ogg_path)
print(f"π₯ Downloaded voice file: {ogg_path}")
sound = AudioSegment.from_ogg(ogg_path)
sound.export(wav_path, format="wav")
print(f"π΅ Converted to WAV: {wav_path}")
transcript = transcribe_with_deepgram(wav_path)
os.remove(ogg_path)
os.remove(wav_path)
if transcript:
cleaned_command = clean_command(transcript)
latest_command = cleaned_command
update.message.reply_text(f"π£ Command received: {latest_command}")
print(f"π₯ New command saved: {latest_command}")
else:
update.message.reply_text("β οΈ Couldn't understand the voice.")
print("β Empty transcript returned from Deepgram")
except Exception as e:
update.message.reply_text("β Voice processing failed.")
print("π¨ Error processing voice message:", e)
def handle_text(update: Update, context: CallbackContext):
global latest_command
cleaned_command = clean_command(update.message.text)
latest_command = cleaned_command
update.message.reply_text(f"β
Got it! Command: {latest_command}")
print(f"π₯ New text command saved: {latest_command}")
# ============== Telegram Bot Runner ==============
def run_bot():
updater = Updater(token=TELEGRAM_TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.voice, handle_voice))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_text))
print("π€ Telegram bot is running...")
updater.start_polling()
updater.idle()
# ============== Main Server Entry ==============
if __name__ == "__main__":
Thread(target=run_bot).start()
print("π Flask server running at http://0.0.0.0:8000")
app.run(host="0.0.0.0", port=8000)