-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from opt-nc/adriens-patch-1
doc(README) : première documentation
- Loading branch information
Showing
2 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |