github.com/sharovik/devbot@v1.0.1-0.20240308094637-4a0387c40516/documentation/schedules.md (about) 1 # Schedules 2 In order to schedule execution of your events, you might trigger `schedule.S.Schedule` method of `schedule` package. You can do that either via migrations OR during the event installation/update 3 4 ## How to 5 First, you need to prepare scenario, with your event name to make sure we execute the right one at the end. PrepareEventScenario method generates the scenario object based on the eventId and reaction type 6 ```go 7 scenario, err := service.PrepareEventScenario(eventID, EventName) 8 if err != nil { 9 //@todo: handle error 10 } 11 ``` 12 Secondary, you need to prepare a scheduled time. You can use the following format: 13 1. `in 1 hour and 2 minutes` 14 2. `1 hour` 15 3. `schedule event examplescenario every 1 minute` 16 4. `23 minutes` OR `in 20 minutes` 17 5. `2022-12-18 11:22` 18 6. `in 1 day` 19 7. `repeat 1 days and at 9:30` 20 8. `Sunday at 10:00` 21 9. `every monday at 9:10` 22 ```go 23 scheduleTime, err := new(schedule.ExecuteAt).FromString(text) 24 if err != nil { 25 //@todo: handle error 26 } 27 ``` 28 Optionally, you can define the `variables` attribute, where you specify variables for your scheduled event. The values should have the same order as it is defined in the target event. 29 As delimiter, you need to use `schedule.VariablesDelimiter`. Example: 30 ``` 31 testValue1;testValue2 32 ``` 33 And finally, you generate the `schedule.Item` object, with the selected event and schedule time. After that, you call `schedule.S.Schedule(item)` method to schedule your event. 34 ```go 35 var item = schedule.Item{ 36 Author: "AUTHOR_ID", 37 Channel: "CHANNEL_ID", 38 ScenarioID: scenario.ID, 39 EventID: scenario.EventID, 40 ReactionType: scenario.EventName, 41 Variables: strings.Join(variables, schedule.VariablesDelimiter), 42 Scenario: scenario, 43 ExecuteAt: scheduleTime, 44 IsRepeatable: false, 45 } 46 ``` 47 ## Example of usage inside the event 48 Below you can see the example of implementation inside the custom event `Execute` method, where we are receiving `message` object, which contain the received chat-message 49 ```go 50 var item = schedule.Item{ 51 Author: message.OriginalMessage.User, 52 Channel: message.Channel, 53 ScenarioID: scenario.ID, 54 EventID: scenario.EventID, 55 ReactionType: scenario.EventName, 56 Variables: strings.Join(variables, schedule.VariablesDelimiter), 57 Scenario: scenario, 58 ExecuteAt: scheduleTime, 59 IsRepeatable: false, 60 } 61 schedule.S.Schedule(item) 62 ```