Skip to content

Commit

Permalink
Merge pull request #1 from opt-nc/adriens-patch-1
Browse files Browse the repository at this point in the history
doc(README) : première documentation
  • Loading branch information
adriens authored Jun 22, 2024
2 parents 10c75ef + 8761241 commit c303aad
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 2 deletions.
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
# mobitag-cli-go
A first GO cli to send moobitags
# ❔ A propos

Cette repo est une **première expérimentation dont le but est de découvrir le
language [`Go`](https://go.dev/)**, sur un cas concret car... c'est plus amusant
et beaucoup plus motivant 🤓.

Cette expérimentation a donc pur but de créer un cli permettant d'envoyer des mobitags
depuis le terminal.

# 🔖 Ressources

- Site web officiel http://www.mobitag.nc
- [🥳 Mobitag.nc... 25 ans plus tard, des sms en SaaS via API{GEE}](https://dev.to/optnc/mobitagnc-25-ans-plus-tard-des-sms-en-saas-via-apigee-2h9e)
- [📲 Mobitag.nc for dummies](https://www.kaggle.com/code/optnouvellecaldonie/mobitag-nc-for-dummies)

# ✅ Prérequis

- [x] Tooling `Go` ([installer `Go`](https://go.dev/doc/install))
- [x] Une clé d'API, chargée dans l'environnement `OPTNC_MOBITAGNC_API_KEY`

# 🚀 Getting started

## ⚙️ Builder

```shell
go build mobitag.go

```

# 🕹️ Essayer

```sh
./mobitag -h
```

```sh
# Tester l'environnement
./mobitag -dry-run

```
# 🥳 Envoyer un `mobit@g`

```sh
/mobitag -to 842984 -message "Hello World : a mobit@g from Go(lang) XD"

```
91 changes: 91 additions & 0 deletions mobitag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)

// 🔑 Get the auth token from the environment variable
var mobitagAPIKey = os.Getenv("OPTNC_MOBITAGNC_API_KEY")

// sendSMS sends an SMS to the specified receiver mobile number
// receiverMobile: the mobile number of the receiver, like 654321
// message: the message to send
func sendSMS(receiverMobile string, message string) {
apiURL := "https://api.opt.nc/mobitag/sendSms"

client := &http.Client{}
req, err := http.NewRequest("POST", apiURL, nil)
if err != nil {
fmt.Printf("An error occurred while creating the request: %v\n", err)
return
}

// set request headers
req.Header.Set("Content-Type", "application/json")
// Authenticate the request
req.Header.Set("x-apikey", mobitagAPIKey)

// set request paylod with receiver mobile number and message
reqBody := fmt.Sprintf(`{"to":"%s","message":"%s"}`, receiverMobile, message)
req.Body = ioutil.NopCloser(strings.NewReader(reqBody))

resp, err := client.Do(req)
if err != nil {
fmt.Printf("❗An error occurred while sending the request: %v\n", err)
return
}
defer resp.Body.Close()

fmt.Printf("ℹ️ Accusé reception: %v\n", resp.Status)
fmt.Printf("📜 Code retour: %v\n", resp.StatusCode)
}

func main() {
// Parse command line arguments
to := ""
message := ""
for i := 1; i < len(os.Args); i++ {
arg := os.Args[i]
switch arg {
// Parse recipient mobile number
case "-t", "-to":
i++
if i < len(os.Args) {
to = os.Args[i]
}
// Parse message
case "-m", "-message":
i++
if i < len(os.Args) {
message = os.Args[i]
}
case "-h", "--help":
fmt.Println("mobitag -t <recipient_mobile> -m <message> [-d] [-v] [-h]")
return
case "-d", "--dry-run":
// Perform dry run test
if mobitagAPIKey != "" {
fmt.Println("✅ Dry run test passed")
} else {
fmt.Println("❌ Dry run test failed: OPTNC_MOBITAGNC_API_KEY environment variable is not set")
}
return
case "-v":
fmt.Println("v0.0")
return
}
}

// Check if required arguments are provided
if to == "" || message == "" {
fmt.Println("❌ Missing required arguments : run <mobitag -h> for help")
return
}

// Send SMS
sendSMS(to, message)
}

0 comments on commit c303aad

Please sign in to comment.