github.com/sharovik/devbot@v1.0.1-0.20240308094637-4a0387c40516/events/eventslist/event.go (about) 1 package eventslist 2 3 import ( 4 "fmt" 5 6 "github.com/sharovik/devbot/internal/database" 7 "github.com/sharovik/devbot/internal/dto/databasedto" 8 "github.com/sharovik/orm/clients" 9 "github.com/sharovik/orm/query" 10 11 "github.com/sharovik/devbot/internal/log" 12 13 "github.com/sharovik/devbot/internal/container" 14 "github.com/sharovik/devbot/internal/dto" 15 ) 16 17 const ( 18 //EventName the name of the event 19 EventName = "eventslist" 20 21 //EventVersion the version of the event 22 EventVersion = "1.0.1" 23 24 helpMessage = `Ask me "events list" and I will return the list of available events, which I use during the messages processing.` 25 ) 26 27 // EventStruct the struct for the event object. It will be used for initialisation of the event in defined-events.go file. 28 type EventStruct struct { 29 } 30 31 // Event - object which is ready to use 32 var ( 33 Event = EventStruct{} 34 m = []database.BaseMigrationInterface{ 35 InsertHelpMessageMigration{}, 36 } 37 ) 38 39 // Help retrieves the help message 40 func (e EventStruct) Help() string { 41 return helpMessage 42 } 43 44 // Alias retrieves the event alias 45 func (e EventStruct) Alias() string { 46 return EventName 47 } 48 49 // Execute method which is called by message processor 50 func (e EventStruct) Execute(message dto.BaseChatMessage) (dto.BaseChatMessage, error) { 51 //This answer will be show once the event get triggered. 52 //Leave message.Text empty, once you need to not show the message, once this event get triggered. 53 c := container.C.Dictionary.GetDBClient() 54 55 q := new(clients.Query). 56 Select([]interface{}{"events.id", "events.alias", "questions.question"}). 57 From(databasedto.EventModel). 58 Join(query.Join{ 59 Target: query.Reference{ 60 Table: databasedto.ScenariosModel.GetTableName(), 61 Key: "event_id", 62 }, 63 With: query.Reference{ 64 Table: databasedto.EventModel.GetTableName(), 65 Key: databasedto.EventModel.GetPrimaryKey().Name, 66 }, 67 Condition: "=", 68 }). 69 Join(query.Join{ 70 Target: query.Reference{ 71 Table: databasedto.QuestionsModel.GetTableName(), 72 Key: "scenario_id", 73 }, 74 With: query.Reference{ 75 Table: databasedto.ScenariosModel.GetTableName(), 76 Key: databasedto.ScenariosModel.GetPrimaryKey().Name, 77 }, 78 Condition: "=", 79 }). 80 Where(query.Where{ 81 First: "questions.question", 82 Operator: "<>", 83 Second: "''", 84 }) 85 res, err := c.Execute(q) 86 if err != nil { 87 message.Text = fmt.Sprintf("Hmm. I tried to get the list of the available events and I failed. Here is the error: ```%s```", err) 88 return message, err 89 } 90 91 message.Text = "Here is the list of the possible phrases which your can use:" 92 for _, item := range res.Items() { 93 message.Text += fmt.Sprintf("\n#%d event: `%s`. Try to ask `%s`. Also you could try to ask `%s --help`", 94 item.GetField("id").Value, 95 item.GetField("alias").Value, 96 item.GetField("question").Value, 97 item.GetField("question").Value, 98 ) 99 } 100 101 return message, nil 102 } 103 104 // Install method for installation of event 105 func (e EventStruct) Install() error { 106 log.Logger().Debug(). 107 Str("event_name", EventName). 108 Str("event_version", EventVersion). 109 Msg("Triggered event installation") 110 111 return container.C.Dictionary.InstallNewEventScenario(database.EventScenario{ 112 EventName: EventName, 113 EventVersion: EventVersion, 114 Questions: []database.Question{ 115 { 116 Question: "events list", 117 Answer: "Just a sec, I will prepare the list for you.", 118 QuestionRegex: "(?i)events list", 119 QuestionGroup: "", 120 }, 121 }, 122 }) 123 } 124 125 // Update for event update actions 126 func (e EventStruct) Update() error { 127 for _, migration := range m { 128 container.C.MigrationService.SetMigration(migration) 129 } 130 131 return container.C.MigrationService.RunMigrations() 132 }