Skip to content

Instantly share code, notes, and snippets.

@mniak
Last active May 18, 2022 18:10
Show Gist options
  • Save mniak/bf7319fc3d24d2a515aac9b965f15d4b to your computer and use it in GitHub Desktop.
Save mniak/bf7319fc3d24d2a515aac9b965f15d4b to your computer and use it in GitHub Desktop.

Skill para Bater Ponto no sistema ADP Expert

Backend

Sugiro hospedar no Vercel.

https://vercel.com/docs/runtimes#official-runtimes/go

Configuração da Skill

Acesse o painel de desenvolvimento da Amazon Alexa, e crie uma skill.

Em Assets > Endpoints, cadastre o seu backend, (algo como http://meubackend.vercel.app/api/ponto) e selecione a opção de certificado com wildcard.

Em Intents > JSON Editor, cole o conteúdo do arquivo skill.json.

É mais ou menos isso. Ainda precisa salver, buildar, publicar, etc. Mas não vou explicar tudo porque nem lembro.

Boa sorte.

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
}
{
"interactionModel": {
"languageModel": {
"invocationName": "controle de ponto",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "Marcar",
"slots": [],
"samples": [
"inicia",
"sai",
"entra",
"sair",
"marca saída",
"entrar",
"marca entrada",
"marca",
"marcar",
"marca o ponto",
"bate o ponto",
"marcar ponto",
"marcar meu ponto",
"marcar o ponto",
"bater ponto",
"bater meu ponto",
"bater o ponto"
]
},
{
"name": "Consultar",
"slots": [],
"samples": [
"Relatório",
"Última marcação",
"Qual o horário da última marcação",
"Quando foi a última marcação",
"Verificar",
"Consulte",
"Consultar"
]
}
],
"types": []
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment