|
package ponto |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
"log" |
|
"net/http" |
|
"strings" |
|
|
|
"github.com/go-resty/resty/v2" |
|
"github.com/mniak/adpexpert" |
|
"github.com/mniak/alexa" |
|
) |
|
|
|
const ( |
|
skillName = "Controle de Ponto" |
|
appID = "amzn1.ask.skill.<SKILL_ID>" |
|
) |
|
|
|
const ( |
|
username = "jair.lula" |
|
password = "abcdefghijklmno" |
|
) |
|
|
|
func Handler(w http.ResponseWriter, r *http.Request) { |
|
handler := alexa.NewSkillBuilder(appID). |
|
SetIgnoreApplicationID(true). |
|
SetIgnoreTimestamp(true). |
|
WithOnSessionStarted(SessionStart). |
|
WithOnIntent(Intent). |
|
BuildHTTPHandler() |
|
|
|
handler(w, r) |
|
} |
|
|
|
func SessionStart(ctx context.Context, req *alexa.Request, session *alexa.Session, context *alexa.Context, resp *alexa.Response) error { |
|
message := fmt.Sprintf("%s iniciado", skillName) |
|
resp.SetOutputText(message) |
|
resp.SetSimpleCard(skillName, message) |
|
resp.ShouldSessionEnd = false |
|
return nil |
|
} |
|
|
|
func Intent(ctx context.Context, req *alexa.Request, session *alexa.Session, context *alexa.Context, resp *alexa.Response) error { |
|
var message string |
|
|
|
switch req.Intent.Name { |
|
case "Marcar": |
|
message, _ = IntentMarcar() |
|
|
|
case "Consultar": |
|
message, _ = IntentConsultar() |
|
default: |
|
message = "Não conheço esta opção" |
|
} |
|
|
|
resp.SetOutputText(message) |
|
resp.SetSimpleCard(skillName, message) |
|
resp.ShouldSessionEnd = true |
|
return nil |
|
} |
|
|
|
func IntentMarcar() (string, bool) { |
|
adpclient := adpexpert.Client{ |
|
Debug: true, |
|
} |
|
|
|
err := adpclient.Login(username, password) |
|
if err != nil { |
|
return fmt.Sprintf("O login falhou: %s", err), false |
|
} |
|
|
|
err = adpclient.PunchIn() |
|
if err != nil { |
|
return fmt.Sprintf("Deu erro: %s", err), false |
|
} |
|
|
|
return "Ponto marcado!", true |
|
} |
|
|
|
func IntentConsultar() (string, bool) { |
|
adpclient := adpexpert.Client{ |
|
Debug: true, |
|
} |
|
|
|
err := adpclient.Login(username, password) |
|
if err != nil { |
|
return fmt.Sprintf("O login falhou: %s", err), false |
|
} |
|
|
|
punches, err := adpclient.GetLastPunches() |
|
if err != nil { |
|
return fmt.Sprintf("Não consegui baixar as marcações: %s", err), false |
|
} |
|
|
|
if len(punches.LastPunches) == 0 { |
|
return "Você não tem marcações recentes", true |
|
} |
|
last := punches.LastPunches[0] |
|
return fmt.Sprintf("A última marcação foi às %d:%02d", last.PunchDateTime.Hour(), last.PunchDateTime.Minute()), true |
|
} |