DEV Community

Cover image for ວິທີສ້າງ API ສົ່ງ SMS OTP ແລະ Verify ໂດຍໃຊ້ Twilio ໃນ Go ແບບງ່າຍໆ
Vongxai xys
Vongxai xys

Posted on

2

ວິທີສ້າງ API ສົ່ງ SMS OTP ແລະ Verify ໂດຍໃຊ້ Twilio ໃນ Go ແບບງ່າຍໆ

ບົດຄວາມນີ້ເຮົາຈະມາລອງສ້າງ api ສົ່ງ sms otp ໄປຫາເບີໂທຜູ້ໃຊ້ໂດຍໃຊ້ twilio ໃນ go ມັນຈະມີວິທີການເຮັດແນວໃດໄປເບິ່ງກັນເລີຍ.


ກ່ອນອື່ນເຮັດລະບົບສົ່ງ otp ເພື່ອຫຍັງ: ຄືອົງກອນຕ່າງໆຈະຫາວິທີຈັດການເພື່ອຕ້ານໄພຄຸກຄາມໃຫ້ມີຊ່ອງໂຫວໜ້ອຍທີ່ສຸດເທົ່າທີຈະເຮັດໄດ້ເພື່ອຄວາມປອດໃຜຕໍ່ລະບົບຂອງຕົນເອງ. ສະນັ້ນການທີ່ຈະໃຫ້ຜູ້ໃຊ້ທົ່ວໄປມາໃຊ້ລະບົບດັ່ງກ່າວເຊັນ: login, register reset password...ອື່ນໆທີ່ມີເປັນຂໍ້ມູນສ່ວນຕົວແນ່ນອນວ່າກະຕ້ອງຫາວິທີຢືນຢັນຕົວຕົນເພື່ອຄວາມປອດໄພໃນລະດັບໜຶ່ງ


ຂັ້ນຕອນ

1/ ເຮົາຕ້ອງໄປສະໝັກ account ຂອງ twilio ສາມາດເຂົ້າໄປສະໝັກຕາມລິ້ງນີ້ເລີຍ www.twilio.com/try-twilio ຖ້າຍັງບໍ່ມີ account

Image description

2/ ຫລັງຈາກນັ້ນ login

Image description

ມັນຈະເຂົ້າໜ້າ www.console.twilio.com ເຂົາຈະໃຫ້ເຮົາສອງ key ຄື: Account SID ແລະ Auth Token ເຮົາຈະເອົາໄປໃສ່ເປັນ parameter config

Image description

3/ ສ້າງ service sms ໃຫ້ໄປຕັ້ງຄ່າຕາມຂັ້ນຕອນໃນຮູບທີ່ບອກໄວ້ເລີຍ

Image description

4/ ໃຫ້ສ້າງ service Friendly name ແມ່ນໃຫ້ຕັ້ງຊື່ sevice name ຂອງເຮົາຕົວຢ່າງໃນນີ້ເຮົາຕັ້ງຊື່ວ່າ OTP demo, Verification channels ແມ່ນໃຫ້ເຮົາຕິກເອົາ sms ເພາະວ່າເຮົາຈະສົ່ງຮູບແບບເປັນ sms, Notes ໃສ່ກໍໄດ້ບໍ່ໃສ່ກໍໄດ້ແລ້ວກົດ continue ໄດ້ເລີຍ

Image description

5/ ຫລັງຈາກທີ່ເຮົາສ້າງ service ສຳເລັດແລ້ວມັນຈະໃຫ້ key Service SID ເຮົາຈະເອົາໄປໃສ່ເປັນ parameter config, Code length ເປັນຈຳນວນຄວາມຍາວຂອງ code OTP ໂດຍປົກະຕິແມ່ນຈະເອົາຕາມຄ່າ default ຂອງມັນກົດ save ໄດ້ເລີຍ

Image description

ູ6/ ຕັ້ງຄ່າ SMS Geographic Permissions ເຊິ່ງຈະເປັນໂຕກຳນົດໂຊນປະເທດໃຫ້ເຮົາກັບໄປໜ້າ console ເລືອກ Vioce -> Settings -> Geo permissions ໃຫ້ເຮົາຄົ້ນຫາພິມຄຳວ່າ laos ມັນອອກມາໃຫ້ກໍຕິກເລືອກແລ້ວກົດ save ຖືວ່າສິ້ນສຸດການຕັ້ງຄ່າເທິ່ງ twilio

Image description

Image description

ຂັ້ນຕອນການສ້າງ api ໃນ Go ກັນແລ້ວໄປລຸຍກັນ


1/ ໃຫ້ເຮົາໄປສ້າງໂປເຈັກເກັບໄວຢູ່ໃສກໍໄດ້ໂດຍເປີດ terminal ໃສ່ຄຳສັ່ງ

mkdir go-otp && cd go-otp
Enter fullscreen mode Exit fullscreen mode

Image description

ຕາມດ້ວຍການສ້າງ Go Modules ໂດຍໃສ່ຄຳສັ່ງ

go mod init {module_name}
Enter fullscreen mode Exit fullscreen mode

2/ ເຮົາຈະໃສ່ echo framework www.echo.labstack.com ໃຫ້ເຮົາສ້າງຊື່ file main.go ໄວ້ເປົ່າໆ ແລະ install echo ໂດຍໃສ່ຄຳສັ່ງ

go get github.com/labstack/echo/v4 
Enter fullscreen mode Exit fullscreen mode

install twilio ໂດຍໃສ່ຄຳສັ່ງ

go get github.com/twilio/twilio-go
Enter fullscreen mode Exit fullscreen mode

3/ install library ສຳລັບ config environment variable.

go get github.com/joho/godotenv
Enter fullscreen mode Exit fullscreen mode

ສ້າງ file .env ໄວ້ໃນໂປເຈັກແລ້ວກຳນົດ key name config

TWILIO_ACCOUNT_SID=Your_accountSID
TWILIO_AUTH_TOKEN=Your_auth_Token
TWILIO_SERVICES_ID=Your_service_SID
Enter fullscreen mode Exit fullscreen mode

ຸ4/ ສ້າງ service api ສົ່ງ sms OTP ກັນເລີຍ

package main

import (
    "context"
    "log"
    "net/http"
    "os"

    "github.com/joho/godotenv"
    "github.com/labstack/echo/v4"
    "github.com/twilio/twilio-go"

    twilioApi "github.com/twilio/twilio-go/rest/verify/v2"
)

func init() {
    if err := godotenv.Load(".env"); err != nil {
        log.Fatal("Error loading .env file")
    }
}

func main() {
    e := echo.New()

    // Routes sendOTP
    e.POST("/sendOTP", func(c echo.Context) error {
        var body struct {
            Phone string `json:"phone"`
        }
        if err := c.Bind(&body); err != nil {
            return c.JSON(http.StatusBadRequest, err)
        }

        sid, err := sendOTP(c.Request().Context(), body.Phone)
        if err != nil {
            return c.JSON(http.StatusInternalServerError, err)
        }

        return c.JSON(http.StatusOK, echo.Map{
            "sid": sid,
            "msg": "OTP sent successfully",
        })
    })

    e.Logger.Fatal(e.Start(":8080"))
}

// client is the Twilio client
var client *twilio.RestClient = twilio.NewRestClientWithParams(twilio.ClientParams{
    Username: os.Getenv("TWILIO_ACCOUNT_SID"),
    Password: os.Getenv("TWILIO_AUTH_TOKEN"),
})

// sendOTP sends OTP to the given phone number
func sendOTP(ctx context.Context, phone string) (string, error) {
    params := &twilioApi.CreateVerificationParams{}
    params.SetTo(phone)
    params.SetChannel("sms")

    resp, err := client.VerifyV2.CreateVerification(os.Getenv("TWILIO_SERVICE_ID"), params)
    if err != nil {
        return "", err
    }

    return *resp.Sid, nil
}


Enter fullscreen mode Exit fullscreen mode

6/ ທົດສອບກັນໂດຍໃຊ້ insonia ຫລື ໃຜຈະໃຊ້ tools ໂຕອື່ນກະໄດ້ຄືກັນບໍ່ວ່າກັນ
localhost:8080/sendOTP ເບີໂທທີ່ສົ່ງໄປຈະຕ້ອງຂຶ້ນນຕົ້ນດ້ວຍ

{
    "phone": "+85620xxxx"
}
Enter fullscreen mode Exit fullscreen mode

Image description

ມັນຕອບກັບມາ status 200 ຖືວ່າສຳເລັດ

{
    "msg": "OTP sent successfully",
    "sid": "VE48ad8c7f47a15d9f4479d899305aaeef"
}
Enter fullscreen mode Exit fullscreen mode

ແລ້ວເຮົາຈະຮູ້ໄດ້ແນວໃດວ່າມັນໄດ້ສົ່ງ sms otp ໄປຫາເບີໂທເຮົາແທ້ໄປກວດກັນ

Image description

ລືມບອກໄປວ່າໃນບົດຄວາມນີ້ເຮົາໃຊ້ account ຟີຣເຊິ່ງທາງ twilio ເຂົາຈະອະນຸຍາດໃຫ້ເຮົາໃຊ້ຟີຣປະມານໜຶ່ງເດືອນທີ່ສາມາດສົ່ງ sms ໄດ້ຖ້າເກີນໜຶ່ງເດືອນແມ່ນບໍ່ໄດ້ແລ້ວ ເຊິ່ງຂອງເຮົາກະໄດ້ໝົດອາຍຸຄືກັນ5555

ກະເລີຍໄດ້ໃຊ້ key account ໂຕທີ່ສະໝັກເສຍເງິນເຮົາບໍ່ສາມາດເປີດໃຫ້ເຫັນ title sms ໄດ້
ຖ້າໃຜທີ່ຍັງບໍ່ເຄີຍສະໝັກແລ້ວໄປສະໝັກແລ້ວເຮັດຕາມຂັ້ນຕອນຕັ້ງຄ່າຕາມທີ່ເຮົາບອກມາດ້ານເທິງແມ່ນສາມາດສົ່ງ sms ໄດ້ແນ່ນອນ

7/ ສ້າງ service api verify OTP

// Routes verifyOTP
    e.POST("/verifyOTP", func(c echo.Context) error {
        var body struct {
            Phone string `json:"phone"`
            Code  string `json:"code"`
        }
        if err := c.Bind(&body); err != nil {
            return c.JSON(http.StatusBadRequest, err)
        }

        if err := verifyOTP(c.Request().Context(), body.Phone, body.Code); err != nil {
            return c.JSON(http.StatusInternalServerError, err)
        }

        return c.JSON(http.StatusOK, echo.Map{
            "msg": "OTP verified successfully",
        })
    })

Enter fullscreen mode Exit fullscreen mode
// verifyOTP verifies the given OTP
func verifyOTP(ctx context.Context, phone, code string) error {
    params := &twilioApi.CreateVerificationCheckParams{}
    params.SetTo(phone)
    params.SetCode(code)

    resp, err := client.VerifyV2.CreateVerificationCheck(os.Getenv("TWILIO_SERVICE_ID"), params)
    if err != nil {
        return err
    }
    if *resp.Status != "approved" {
        return errors.New("invalid code")
    }

    return nil
}
Enter fullscreen mode Exit fullscreen mode

8/ ສຸດທ້າຍຂໍທ້າຍສຸດດ້ວຍການທົດສອບ verify otp

{
    "phone": "+85620xxxx,
    "code": "143464"
}
Enter fullscreen mode Exit fullscreen mode

Image description

ມັນຕອບກັບມາ status 200 ສະແດງວ່າ verify ຖືກຕ້ອງເຢ້ໆ5555

{
    "msg": "OTP verified successfully"
}
Enter fullscreen mode Exit fullscreen mode

ມາຮອດນີ້ຖືວ່າໄດ້ສຳເລັດແລ້ວກັບການສ້າງ API ສົ່ງ SMS OTP ແລະ Verify ໂດຍໃຊ້ Twilio ໃນ Go ແບບງ່າຍໆ

ຖ້າມີຂໍ້ຜິດພາດປະການໃດ ຫລື ມີສິ່ງໃດຢາກນຳແນະກໍແນະນຳໄດ້ເດີ້ ຜູ້ຂຽນເອງຫາກໍລອງໃຊ້

ຈົດໄວ້ຢ້ານລືມ5555

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more