Created
May 22, 2024 03:48
-
-
Save MarcBittner/38e7c156991023eedbdd8c02d7a9d42a to your computer and use it in GitHub Desktop.
api stuffs
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
package main | |
import ( | |
"encoding/json" | |
"net/http" | |
"os" | |
"github.com/sendgrid/sendgrid-go" | |
"github.com/sendgrid/sendgrid-go/helpers/mail" | |
"github.com/twilio/twilio-go" | |
// "github.com/twilio/twilio-go/client" | |
openapi "github.com/twilio/twilio-go/rest/api/v2010" | |
) | |
type MessageRequest struct { | |
To string `json:"to"` | |
Body string `json:"body"` | |
Subject string `json:"subject,omitempty"` // Optional, for LinkedIn | |
} | |
func main() { | |
http.HandleFunc("/send/sms", sendSMSMessage) | |
http.HandleFunc("/send/linkedin", sendLinkedInMessage) | |
http.HandleFunc("/send/email", sendEmailMessage) | |
http.ListenAndServe(":8080", nil) | |
} | |
func sendSMSMessage(w http.ResponseWriter, r *http.Request) { | |
var request MessageRequest | |
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
return | |
} | |
client := twilio.NewRestClientWithParams(twilio.ClientParams{ | |
Username: os.Getenv("TWILIO_ACCOUNT_SID"), | |
Password: os.Getenv("TWILIO_AUTH_TOKEN"), | |
}) | |
// params := &openapi.CreateMessageParams{} | |
// params.SetTo(request.To) | |
// params.SetFrom(os.Getenv("TWILIO_PHONE_NUMBER")) | |
// params.SetBody(request.Body) | |
params := &openapi.CreateMessageParams{} | |
params.SetTo(os.Getenv("TO_PHONE_NUMBER")) | |
params.SetFrom(os.Getenv("TWILIO_PHONE_NUMBER")) | |
params.SetBody("Hello from Golang!") | |
// _, err := client.ApiV2010.CreateMessage(params) | |
_, err := client.Api.CreateMessage(params) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
w.WriteHeader(http.StatusOK) | |
json.NewEncoder(w).Encode(map[string]string{"status": "SMS message sent"}) | |
} | |
func sendLinkedInMessage(w http.ResponseWriter, r *http.Request) { | |
var request MessageRequest | |
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
return | |
} | |
// Placeholder for LinkedIn message sending logic | |
// Too complex for now | |
w.WriteHeader(http.StatusNotImplemented) | |
json.NewEncoder(w).Encode(map[string]string{"status": "LinkedIn message sending not implemented"}) | |
} | |
func sendEmailMessage(w http.ResponseWriter, r *http.Request) { | |
var request MessageRequest | |
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
return | |
} | |
from := mail.NewEmail(os.Getenv("SENDGRID_FROM_USERNAME"), os.Getenv("SENDGRID_FROM_EMAIL")) | |
to := mail.NewEmail("Recpient", request.To) | |
message := mail.NewSingleEmail(from, request.Subject, to, request.Body, request.Body) | |
client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY")) | |
_, err := client.Send(message) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
w.WriteHeader(http.StatusOK) | |
json.NewEncoder(w).Encode(map[string]string{"status": "Email sent"}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment