forked from urdt/ussd
format the number to a standard version
This commit is contained in:
parent
267132b956
commit
f666b1e960
@ -20,6 +20,7 @@ import (
|
|||||||
"git.defalsify.org/vise.git/logging"
|
"git.defalsify.org/vise.git/logging"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
|
|
||||||
|
"git.grassecon.net/urdt/ussd/common"
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
"git.grassecon.net/urdt/ussd/config"
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
"git.grassecon.net/urdt/ussd/initializers"
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
||||||
@ -76,7 +77,13 @@ func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
|
|||||||
return "", fmt.Errorf("no phone number found")
|
return "", fmt.Errorf("no phone number found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return phoneNumber, nil
|
formattedNumber, err := common.FormatPhoneNumber(phoneNumber)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %v\n", err)
|
||||||
|
return "", fmt.Errorf("failed to format number")
|
||||||
|
}
|
||||||
|
|
||||||
|
return formattedNumber, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (arp *atRequestParser) GetInput(rq any) ([]byte, error) {
|
func (arp *atRequestParser) GetInput(rq any) ([]byte, error) {
|
||||||
|
@ -3,6 +3,7 @@ package common
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Define the regex patterns as constants
|
// Define the regex patterns as constants
|
||||||
@ -46,3 +47,17 @@ func CheckRecipient(recipient string) (string, error) {
|
|||||||
|
|
||||||
return "", fmt.Errorf("invalid recipient: must be a phone number, address or alias")
|
return "", fmt.Errorf("invalid recipient: must be a phone number, address or alias")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FormatPhoneNumber formats a Kenyan phone number to "254xxxxxxxx".
|
||||||
|
func FormatPhoneNumber(phone string) (string, error) {
|
||||||
|
// Remove any leading "+" and spaces
|
||||||
|
phone = strings.TrimPrefix(phone, "+")
|
||||||
|
phone = strings.ReplaceAll(phone, " ", "")
|
||||||
|
|
||||||
|
// Replace leading "0" with "254" if present
|
||||||
|
if strings.HasPrefix(phone, "0") {
|
||||||
|
phone = "254" + phone[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return phone, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user