github.com/sharovik/devbot@v1.0.1-0.20240308094637-4a0387c40516/events/cancelscenario/event.go (about) 1 package cancelscenario 2 3 import ( 4 "github.com/sharovik/devbot/internal/database" 5 "github.com/sharovik/devbot/internal/helper" 6 "github.com/sharovik/devbot/internal/log" 7 8 "github.com/sharovik/devbot/internal/container" 9 "github.com/sharovik/devbot/internal/dto" 10 ) 11 12 const ( 13 //EventName the name of the event 14 EventName = "cancelscenario" 15 16 //EventVersion the version of the event 17 EventVersion = "1.0.0" 18 19 helpMessage = "Write me ```stop conversation #channel-name|@username``` and I will stop any conversation which is started for it." 20 21 regexChannel = `(?im)(?:[<#@]|(?:<))(\w+)(?:[|>]|(?:>))` 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 channel := extractChannelName(message.OriginalMessage.Text) 44 if channel == "" { 45 message.Text = "Please specify the channel name." 46 return message, nil 47 } 48 49 //This answer will be show once the event get triggered. 50 //Leave message.Text empty, once you need to not show the message, once this event get triggered. 51 message.Text = "Done." 52 return message, nil 53 } 54 55 // Install method for installation of event 56 func (e EventStruct) Install() error { 57 log.Logger().Debug(). 58 Str("event_name", EventName). 59 Str("event_version", EventVersion). 60 Msg("Triggered event installation") 61 62 return container.C.Dictionary.InstallNewEventScenario(database.EventScenario{ 63 EventName: EventName, 64 EventVersion: EventVersion, 65 Questions: []database.Question{ 66 { 67 Question: "stop conversation", 68 Answer: "Ok, will do it now.", 69 QuestionRegex: "(?i)(stop conversation)", 70 QuestionGroup: "", 71 }, 72 }, 73 }) 74 } 75 76 // Update for event update actions 77 func (e EventStruct) Update() error { 78 return nil 79 } 80 81 func extractChannelName(text string) string { 82 matches := helper.FindMatches(regexChannel, text) 83 if len(matches) == 0 { 84 return "" 85 } 86 87 if matches["1"] == "" { 88 return "" 89 } 90 91 return matches["1"] 92 }