From 688602cf179a5a13e7732822f5859615c008b767 Mon Sep 17 00:00:00 2001 From: SharkFourSix <18681730+SharkFourSix@users.noreply.github.com> Date: Fri, 8 Jul 2022 01:34:08 +0200 Subject: [PATCH] =?UTF-8?q?Add=20timeount=20configuration=20=E2=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +++ dialoguss.go | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6c3ae4f..fe9fafd 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,6 @@ ENV/ # mypy .mypy_cache/ + +.vscode/ +dialoguss \ No newline at end of file diff --git a/dialoguss.go b/dialoguss.go index de74852..5b8ac20 100644 --- a/dialoguss.go +++ b/dialoguss.go @@ -92,6 +92,7 @@ type Session struct { url string client *http.Client ApiType string + Timeout time.Duration } type DialogussConfig struct { @@ -99,6 +100,7 @@ type DialogussConfig struct { Dial string `yaml:"dial"` PhoneNumber string `yaml:"phoneNumber"` Sessions []Session `yaml:"sessions"` + Timeout int `yaml:"timeout"` } /// AddStep adds step to session @@ -112,6 +114,10 @@ func NewInteractiveSession(d DialogussConfig) *Session { if trurouteMode { apiType = ApiTypeTruroute } + var sessionTimeout = defaultTimeout + if d.Timeout > 0 { + sessionTimeout = time.Duration(d.Timeout) * time.Second + } return &Session{ ID: fmt.Sprintf("DialogussSession__%d", rand.Uint64()), PhoneNumber: d.PhoneNumber, @@ -121,6 +127,7 @@ func NewInteractiveSession(d DialogussConfig) *Session { url: d.URL, client: &http.Client{}, ApiType: apiType, + Timeout: sessionTimeout, } } @@ -197,7 +204,7 @@ sessionLoop: select { case value := <-inputCh: input = value - case <-time.After(defaultTimeout): + case <-time.After(s.Timeout): fmt.Println("Session timed out!") break sessionLoop } @@ -227,7 +234,7 @@ type Dialoguss struct { /// LoadConfig loads configuration from YAML func (d *Dialoguss) LoadConfig() error { - d.config = DialogussConfig{} + d.config = DialogussConfig{Timeout: int(defaultTimeout)} b, err := ioutil.ReadFile(d.file) if err != nil { return err