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

     1  package example
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/sharovik/devbot/internal/database"
     7  
     8  	"github.com/sharovik/devbot/internal/log"
     9  
    10  	"github.com/sharovik/devbot/internal/container"
    11  	"github.com/sharovik/devbot/internal/dto"
    12  )
    13  
    14  const (
    15  	//EventName the name of the event
    16  	EventName = "example"
    17  
    18  	//EventVersion the version of the event
    19  	EventVersion = "1.0.1"
    20  
    21  	helpMessage = "Ask me `who are you?` and you will see the answer."
    22  )
    23  
    24  // EventStruct the struct for the event object. It will be used for initialisation of the event in defined-events.go file.
    25  type EventStruct struct {
    26  }
    27  
    28  // Event - object which is ready to use
    29  var Event = EventStruct{}
    30  
    31  // Help retrieves the help message
    32  func (e EventStruct) Help() string {
    33  	return helpMessage
    34  }
    35  
    36  // Alias retrieves the event alias
    37  func (e EventStruct) Alias() string {
    38  	return EventName
    39  }
    40  
    41  // Execute method which is called by message processor
    42  func (e EventStruct) Execute(message dto.BaseChatMessage) (dto.BaseChatMessage, error) {
    43  	//This answer will be show once the event get triggered.
    44  	//Leave message.Text empty, once you need to not show the message, once this event get triggered.
    45  	message.Text = "This is an example of the answer."
    46  	return message, nil
    47  }
    48  
    49  // Install method for installation of event
    50  func (e EventStruct) Install() error {
    51  	log.Logger().Debug().
    52  		Str("event_name", EventName).
    53  		Str("event_version", EventVersion).
    54  		Msg("Triggered event installation")
    55  
    56  	return container.C.Dictionary.InstallNewEventScenario(database.EventScenario{
    57  		EventName:    EventName,
    58  		EventVersion: EventVersion,
    59  		Questions: []database.Question{
    60  			{
    61  				Question:      "who are you?",
    62  				Answer:        fmt.Sprintf("Hello, my name is %s", container.C.Config.MessagesAPIConfig.BotName),
    63  				QuestionRegex: "(?i)who are you?",
    64  				QuestionGroup: "",
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  // Update for event update actions
    71  func (e EventStruct) Update() error {
    72  	return nil
    73  }