github.com/sharovik/devbot@v1.0.1-0.20240308094637-4a0387c40516/events/listopenconversations/event.go (about)

     1  package listopenconversations
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/sharovik/devbot/internal/service/message/conversation"
     7  
     8  	"github.com/sharovik/devbot/internal/database"
     9  	"github.com/sharovik/devbot/internal/log"
    10  
    11  	"github.com/sharovik/devbot/internal/container"
    12  	"github.com/sharovik/devbot/internal/dto"
    13  )
    14  
    15  const (
    16  	//EventName the name of the event
    17  	EventName = "listopenconversations"
    18  
    19  	//EventVersion the version of the event
    20  	EventVersion = "1.0.0"
    21  
    22  	helpMessage = "Write me ```show open conversations``` and I will show you the list of open conversations."
    23  )
    24  
    25  // EventStruct the struct for the event object. It will be used for initialisation of the event in defined-events.go file.
    26  type EventStruct struct {
    27  }
    28  
    29  // Event - object which is ready to use
    30  var Event = EventStruct{}
    31  
    32  // Help retrieves the help message
    33  func (e EventStruct) Help() string {
    34  	return helpMessage
    35  }
    36  
    37  // Alias retrieves the event alias
    38  func (e EventStruct) Alias() string {
    39  	return EventName
    40  }
    41  
    42  // Execute method which is called by message processor
    43  func (e EventStruct) Execute(message dto.BaseChatMessage) (dto.BaseChatMessage, error) {
    44  	currentConversations := conversation.GetCurrentConversations()
    45  	if len(currentConversations) == 0 {
    46  		message.Text = "There is no open conversations."
    47  		return message, nil
    48  	}
    49  
    50  	message.Text = "Here is the list:"
    51  	for _, conv := range conversation.GetCurrentConversations() {
    52  		message.Text += "\n-------"
    53  		message.Text += fmt.Sprintf("\nScenario #%d was triggered in <@%s> chat", conv.ScenarioID, conv.LastQuestion.Channel)
    54  		if len(conv.Scenario.RequiredVariables) == 0 {
    55  			message.Text += "\nAnd there is no answers received yet for that scenario."
    56  		} else {
    57  			message.Text += "\nWith the next filled answers:"
    58  			for _, variable := range conv.Scenario.RequiredVariables {
    59  				message.Text += fmt.Sprintf("\n* %s: `%s`", variable.Question, variable.Value)
    60  			}
    61  		}
    62  		message.Text += "\n-------"
    63  	}
    64  
    65  	return message, nil
    66  }
    67  
    68  // Install method for installation of event
    69  func (e EventStruct) Install() error {
    70  	log.Logger().Debug().
    71  		Str("event_name", EventName).
    72  		Str("event_version", EventVersion).
    73  		Msg("Triggered event installation")
    74  
    75  	return container.C.Dictionary.InstallNewEventScenario(database.EventScenario{
    76  		EventName:    EventName,
    77  		EventVersion: EventVersion,
    78  		Questions: []database.Question{
    79  			{
    80  				Question:      "show open conversations",
    81  				Answer:        "Give me a sec.",
    82  				QuestionRegex: "(?i)(show open conversations)",
    83  				QuestionGroup: "",
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  // Update for event update actions
    90  func (e EventStruct) Update() error {
    91  	return nil
    92  }