package main
import (
"encoding/json"
"log"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/plivo/plivo-go/v7/xml"
)
// init gets called before the main function
func init() {
// Log error if .env file does not exist
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
}
// Handle incoming calls to a Plivo number, connect agent with customer and vice versa without revealing their actual phone numbers.
func main() {
r := gin.Default()
r.GET("/number_masking", func(c *gin.Context) {
customerAgentmap := os.Getenv("BASEMAP")
// Declares an empty map interface
var result map[string]string
fromNumber := c.Query("From")
toNumber := c.Query("To")
// Unmarshal or Decode the JSON to the interface.
json.Unmarshal([]byte(customerAgentmap), &result)
agentCustomermap := reverseMap(result)
_, custToagent := result[fromNumber]
_, agenTocust := agentCustomermap[fromNumber]
if custToagent { // Check whether the customer's number is in the customer-agent mapping
destNumber := result[fromNumber] // Assign the value from the customer-agent array to number variable
c.XML(200, xml.ResponseElement{
Contents: []interface{}{
new(xml.DialElement).
SetCallerID(toNumber). // Plivo number is used as the caller ID for the call toward the agent
SetContents([]interface{}{
new(xml.NumberElement).
SetContents(destNumber),
}),
},
})
} else if agenTocust { // Check whether the agent's number is in the customer-agent mapping
destNumber := agentCustomermap[fromNumber] // Assign the key from the customer-agent array to number variable
c.XML(200, xml.ResponseElement{
Contents: []interface{}{
new(xml.DialElement).
SetCallerID(toNumber). // Plivo number is used as the caller ID for the call toward the customer
SetContents([]interface{}{
new(xml.NumberElement).
SetContents(destNumber),
}),
},
})
}
c.Header("Content-Type", "application/xml")
})
r.Run() // listen and serve on 0.0.0.0:8080 (for Windows "localhost:8080")
}
// Reverse the Basemap from env file to get customer-agent mapping data
func reverseMap(m map[string]string) map[string]string {
n := make(map[string]string)
for k, v := range m {
n[v] = k
}
return n
}