-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
58 lines (46 loc) · 1.72 KB
/
api.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
from flask import Flask, request
import requests
app = Flask(__name__)
@app.route("/")
def index():
return "Hello, world!"
@app.route("/generate-das", methods=["POST"])
def generate_das():
# Get the required information from the request body
nome = request.json["nome"]
cnpj = request.json["cnpj"]
whatsapp = request.json["whatsapp"]
# Get the MEI's information from the Receita Federal API
response = requests.get("https://api.receita.fazenda.gov.br/v1/mei/cnpj/{}/info".format(cnpj))
if response.status_code == 200:
mei_info = response.json()
else:
raise Exception("Não foi possível obter as informações do MEI.")
# Generate the DAS
das = generate_das(mei_info)
# Send the DAS to the WhatsApp number
send_das_to_whatsapp(das, whatsapp)
return "O DAS foi gerado e enviado para o WhatsApp."
def generate_das(mei_info):
# Get the DAS information from the Receita Federal API
response = requests.get("https://api.receita.fazenda.gov.br/v1/mei/cnpj/{}/das".format(mei_info["cnpj"]))
if response.status_code == 200:
das_info = response.json()
else:
raise Exception("Não foi possível obter as informações do DAS.")
# Create the DAS document
das = {
"nome": mei_info["nome"],
"cnpj": mei_info["cnpj"],
"valor": das_info["valor"],
"vencimento": das_info["vencimento"],
}
return das
def send_das_to_whatsapp(das, whatsapp):
# Send the DAS document to the WhatsApp number
requests.post("https://api.whatsapp.com/v1/send/message", data={
"chat_id": whatsapp,
"text": "Olá, [nome]. Aqui está o seu DAS: \n\n[das]"
})
if __name__ == "__main__":
app.run(debug=True)