-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
59 lines (45 loc) · 1.83 KB
/
main.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
import os
import re
from dotenv import load_dotenv
from fastapi import FastAPI
from otp.melipayamak import Api
app = FastAPI()
load_dotenv()
api = Api(os.getenv("OTP_USERNAME"), os.getenv("OTP_PASSWORD"))
apiSoap = api.sms("soap")
apiRest = api.sms("rest")
_from = os.getenv("OTP_PHONE_NUMBER")
@app.get("/")
async def root():
return {"message": "Welcome to Bimeh Sabz OTP Service! What are you locking for ?"}
@app.get("/getCredit")
async def get_credit():
return apiSoap.get_credit()
@app.get("/getBasePrice")
async def get_base_price():
return apiRest.get_base_price()
@app.post("/sendSMS")
async def send_sms(msg: str = "", number: str = ""):
if msg == "":
return {"status": "error", "msg": "empty sms body!"}
else:
if number == "":
return {"status": "error", "msg": "empty phone number!"}
else:
if re.search("^09[0-9][0-9]{8}$", number):
res_id = ""
try:
res_id = apiSoap.send(number, _from, msg)
print(f"INFO: SMS send with id {res_id} with soap")
return {"status": "success", "msg": "done!"}
except:
print(f"INFO: Error while sending SMS with soap service! - id: {res_id} - ConnectionError")
try:
res_id = apiRest.send(number, _from, msg)
print(f"INFO: SMS send with id {res_id} with rest")
return {"status": "success", "msg": "done!"}
except:
print(f"INFO: Error while sending SMS with rest service! - id: {res_id} - ConnectionError")
return {"status": "error", "msg": "message not delivered!"}
else:
return {"status": "error", "msg": "wrong phone number"}