Automate calls using asterisk
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.1 KiB

3 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "os/user"
  8. "path/filepath"
  9. )
  10. // Reference from:
  11. // https://wiki.asterisk.org/wiki/display/AST/Asterisk+Call+Files
  12. // Config all configurations for this application.
  13. type Config struct {
  14. HTTPBind string `json:"http_bind"`
  15. HTTPPort uint `json:"http_port"`
  16. HTTPDebug bool `json:"http_debug"`
  17. HTTPSystemDSocket bool `json:"http_systemd_socket"`
  18. AsteriskSpoolDir string `json:"asterisk_spool_dir"`
  19. DefaultChannel string `json:"default_channel"`
  20. DefaultCallerId string `json:"default_caller_id"`
  21. DefaultWaitTime uint64 `json:"default_wait_time"` // 5 seconds per ring.
  22. DefaultMaxRetries uint64 `json:"default_max_retries"`
  23. DefaultRetryTime uint64 `json:"default_retry_time"`
  24. DefaultAccount string `json:"default_account"`
  25. DefaultApplication string `json:"default_application"`
  26. DefaultData string `json:"default_data"`
  27. PreventAPIApplication bool `json:"prevent_api_application"` // For security, prevent applications from being executed via API call.
  28. DefaultContext string `json:"default_context"`
  29. DefaultExtension string `json:"default_extension"`
  30. DefaultPriority string `json:"default_priority"`
  31. DefaultSetVar map[string]string `json:"default_set_var"`
  32. DefaultArchive bool `json:"default_archive"`
  33. APIToken string `json:"api_token"`
  34. }
  35. // ReadConfig read the configuration file into the config structure of the app.
  36. func (a *App) ReadConfig() {
  37. // Get our current user for use in determining the home path.
  38. usr, err := user.Current()
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. // Different configuration file paths.
  43. localConfig, _ := filepath.Abs("./config.json")
  44. homeDirConfig := usr.HomeDir + "/.config/asterisk-outgoing-call-api/config.json"
  45. etcConfig := "/etc/asterisk/outgoing-call-api.json"
  46. // Store defaults first.
  47. app.config = Config{
  48. HTTPPort: 9747,
  49. HTTPDebug: false,
  50. HTTPSystemDSocket: false,
  51. AsteriskSpoolDir: "/var/spool/asterisk",
  52. PreventAPIApplication: true,
  53. DefaultArchive: false,
  54. }
  55. // Determine which config file to use.
  56. var configFile string
  57. if _, err := os.Stat(app.flags.ConfigPath); err == nil && app.flags.ConfigPath != "" {
  58. configFile = app.flags.ConfigPath
  59. } else if _, err := os.Stat(localConfig); err == nil {
  60. configFile = localConfig
  61. } else if _, err := os.Stat(homeDirConfig); err == nil {
  62. configFile = homeDirConfig
  63. } else if _, err := os.Stat(etcConfig); err == nil {
  64. configFile = etcConfig
  65. } else {
  66. log.Fatal("Unable to find a configuration file.")
  67. }
  68. // Read the config file.
  69. jsonFile, err := ioutil.ReadFile(configFile)
  70. if err != nil {
  71. log.Fatalf("Error reading JSON file: %v\n", err)
  72. }
  73. // Parse the config file into the configuration structure.
  74. err = json.Unmarshal(jsonFile, &app.config)
  75. if err != nil {
  76. log.Fatalf("Error parsing JSON file: %v\n", err)
  77. }
  78. }