Timmy
Timmy is a Pascal unit for creating chat bots. It provides the TTimmy object, which is a data type that you can assign your variables to to make bots. Once that is done, you can start adding keywords for messages, and get the bot answers to the end user’s messages.
Creating bots with Timmy is as easy as 1 2 3.
How to create a bot with Timmy
- Declare
timmyunit - Declare a
TTimmyvariable - Initialize your
TTimmy - Add some keywords for your messages, and possible replies to those messages
- Start answering
Example program
Program MyFirstBot;
Uses timmy;
Var MyBot: TTimmy;
BEGIN
MyBot.Init(70, 'I gave up', False);
MyBot.Add(StrSplit('Hello', ' '), StrSplit('Greetings!|Hello to you!|Hi!', '|'));
MyBot.Add('How are you', 'I am fine!;Never better!;I''m doing great.');
MyBot.Add('What 2 + 2', 'The answer is 4', ' ', '@');
Writeln(MyBot.Answer('Hello!'));
Writeln(MyBot.Answer('How are you?'));
Writeln(MyBot.Answer('What is 2 + 2?'));
Writeln(MyBot.Answer('What is the meaning of life?'));
END.
If we compile and execute the program, we should get something like this as the output:
Hi!
I'm doing great
The answer is 4
I gave up.
For more examples, check out the examples folder.
Variables, functions and procedures of TTimmy
constructor Init(Percent: Integer; DefaultRep: String; DpCheck: Boolean)
- Source: Line 143 (reference)
- Parameters:
Percent[Integer]: Desired initial value forTTimmy.TPercentDefaultRep[String]: Initial value forTTimmy.NoUdstdRepDpCheck[Boolean]: Initial value forTTimmy.DupesCheck
- Availability: v1.2.0
- Description: Constructor of the
TTimmyinstance, which prepares the instance for being used. In this constructor,TTimmy.TPercent,TTimmy.NoUdstdRep, andTTimmy.DupesCheckget assigned to the values of the argumentsPercent,DefaultRepandDpCheck, respectively.TTimmy.Enabledis set to true, and the bot starts with nothing in its metadata. You must run this constructor before performing any other operation with your bot instance, or else the bot won’t work properly.
function Init()
- Source:
- Parameters: None
- Return: Integer
- 101: The instance is already initialized
- 100: The operation is successful
- Availability: v1.0.0 to v1.1.0
- Description: Acts like v1.2.0’s
TTimmy.Init()constructor, but this one is a function.TTimmy.TPercent,TTimmy.NoUdstdRep,TTimmy.DupesCheckget assigned to 70, “Sorry, I didn’t get that”, andTrue, respectively. You must run this function before performing any other operation with your bot instance, or else the bot won’t work properly.
Enabled
- Type: Boolean variable
- Visibility: Private
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.Enabledtells other functions ofTTimmywhether if the bot instance is ready to work. IfTTimmy.Enabledis false, all major functions ofTTimmywon’t perform their operations and will exit right away, usually with the return code 102. The value of this boolean variable can be set by usingTTimmy.Enable()orTTimmy.Disable().
procedure Enable()
- Source: Line 154 (reference)
- Parameters: None
- Visibility: Public
- Availability: v1.2.0
- Description: Procedure that sets
TTimmy.Enabledto true. In other words, it enables the bot.
procedure Disable()
- Source: Line 158 (reference)
- Parameters: None
- Visibility: Public
- Availability: v1.2.0
- Description: Procedure that sets
TTimmy.Enabledto false, as oppose ofTTimmy.Enable(). You may disable the bot if you somehow want it to stop working temporarily.
MKeywordsList
- Type: Dynamic array of
TStrArray - Visibility: Private
- Availability: v1.0.0 to v1.2.0 (In v1.0.0, it’s
QKeywordsList) - Description: This is an array of arrays. Each array in this array holds strings, which are keywords for a message. Keywords help the bot (or more specifically,
TTimmy.Answer()) to understand the end-user’s messages and have replies for the messages.
ReplyList
- Type: Dynamic array of
TStrArray - Visibility: Private
- Availability: v1.0.0 to v1.2.0
- Description: Just like
TTimmy.MKeywordsList,TTimmy.ReplyListis an array of arrays. Each array inTTimmy.ReplyListholds strings, which are possible replies for a message. If an array inTTimmy.ReplyListhas more than two strings, the bot will pick one, randomly. Arrays inTTimmy.ReplyListare correspond to arrays inTTimmy.MKeywordsListin terms of position. For example, the replies at offset 2 ofTTimmy.ReplyListare replies for the message with the keywords at offset 2 ofTTimmy.MKeywordsList.
NOfEntries
- Type: Integer
- Visibility: Private
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.NOfEntriesis the number of elements inTTimmy.MKeywordsList, orTTimmy.ReplyList(the length ofTTimmy.MKeywordsListis the same as the length ofTTimmy.ReplyListat all times anyway). We implement this instead of doingLength(MKeywordsList)(orLength(ReplyList)) because it’s more convenient.
procedure Update()
- Source:
- Parameters: None
- Visibility: Private
- Availability: v1.0.0 to v1.2.0
- Description: Procedure that sets the lengths of
TTimmy.MKeywordsListandTTimmy.ReplyListtoTTimmy.NOfEntries. This procedure is called whenever the bot takes or remove data within its metadata (either byTTimmy.Add()orTTimmy.Remove()), in which the length ofTTimmy.MKeywordsList(andTTimmy.ReplyListas well) is changed.
TPercent
- Type: Integer
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.TPercentspecifies the minimum percentage of the number of matching keywords over the total number of words in the message that the bot needs in order to “understand” the message.
NoUdstdRep
- Type: String
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.NoUdstdRepis the default reply of the bot. It is returned toTTimmy.Answer()whenever the bot does not “understand” the user’s message.
function Add(MKeywords, Replies: TStrArray)
- Source:
- Parameters:
MKeywords(QKeywordsin v1.0.0) [TStrArray]: New keywords clue for a messageReplies[TStrArray]: Possible replies to the message that contains the keywords clue inMKeywords
- Return: Integer
- 102: The instance is not enabled (or initialized)
- 202: Duplication check is enabled (
TTimmy.DupesCheck= true) and a match ofMKeywordsis presented inTTimmy.MKeywordsList - 200: The operation is successful
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.Add()adds new data to the bot’s metadata, which means it addsMKeywordstoTTimmy.MKeywordsListandRepliestoTTimmy.ReplyList. It takes two arguments, one is keywords clue for a message, and two is possible responses to that message.TTimmy.Add()is overloaded and this implementation of it is considered the original implementation.
function Add(KeywordsStr, RepStr: String)
- Source:
- Parameters:
KeywordsStr[String]: New keywords clue for a message, joined by the space characterRepStr[String]: Possible replies to the message that contains the keywords presented inKeywordsStr, joined by the semicolon
- Return: The same as the original implementation of
TTimmy.Add(). - Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description: Yet another function to add new data, but this one takes string arguments instead of
TStrArrays. These strings will then be delimited using a delimiter with the help of theStrSplit()function to formTStrArrays, and theseTStrArrays will be passed over to the original implementation ofTTimmy.Add().
function Add(KeywordsStr, RepStr: String; KStrDeli, MStrDeli: Char)
- Source:
- Parameters:
KeywordsStr[String]: New keywords clue for a message, joined byKStrDeliRepStr[String]: Possible replies to the message that contains the keywords presented inKeywordsStr, joined byMStrDeliKStrDeli[Character]: Delimiter forKeywordsStrMStrDeli(QStrDeliin v1.0.0) [Character]: Delimiter forRepStr
- Return: The same as the original implementation of
TTimmy.Add(). - Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description: This implementation of
TTimmy.Add()is the same as the above one (which takes string arguments). However, the difference is, this one allows you to use custom delimiters for the strings.
DupesCheck
- Type: Boolean
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.DupesCheckspecifies whetherTTimmy.Add()should check for duplicate before adding new data or not. If the new data matches any of those already persisted inTTimmy.ReplyList,TTimmy.Add()stops its operation and returns 202, indicating that the operation is not successful.
function Remove(MKeywords: TStrArray)
- Source:
- Parameters:
MKeywords(QKeywordsin v1.0.0) [TStrArray]: Keywords clue to delete from the bot’s metadata. - Return: Integer
- 102: The instance is not enabled (or initialized)
- 308: The operation is successful
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description:
TTimmy.Remove()removes data from the bot’s metadata, and as of version 1.2.0, there are 4 overloadedTTimmy.Remove(). This one takes aTStrArray, find matching arrays (arrays with the same elements in the same order) inTTimmy.MKeywordsList, and delete those matching arrays. It also deletes the array(s) inReplyListthat is/are bond to those matching array(s) of keywords.
function Remove(KeywordsStr: String)
- Source:
- Parameters:
KeywordsStr[String]: Keywords clue to delete from the bot’s metadata, joined by the space character. - Return: The same as the above implementation of
TTimmy.Remove(). - Visibility: Public
- Availability: v1.1.0 to v1.2.0
- Description: Another overloaded
TTimmy.Remove(), this one takes one and only string argument. The string is then delimited using the space character to get aTStrArrayoutput, and thisTStrArrayis processed by the aboveTTimmy.Remove(MKeywords: TStrArray).
function Remove(KeywordsStr: String; KStrDeli: Char)
- Source:
- Parameters:
KeywordsStr[String]: Keywords clue to delete from the bot’s metadata, joined byKStrDeliKStrDeli[Character]: Delimiter forKeywordsStr
- Return: The same as the top implementation of
TTimmy.Remove()(TTimmy.Remove(MKeywords: TStrArray)). - Visibility: Public
- Availability: v1.1.0 to v1.2.0
- Description: Yet another overloaded
TTimmy.Remove()which is quite similar to the above one, but this one allows you to use any delimiter you like instead of the space character.
function Remove(AIndex: Integer)
- Source:
- Parameters:
AIndex[Integer]: Offset of elements inTTimmy.MKeywordsListandTTimmy.ReplyListthat need to be removed - Return: Integer
- 305:
AIndexis an invalid offset inTTimmy.MKeywordsList(orTTimmy.ReplyList) - 300: The operation is successful
- 305:
- Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description: This is the major implementation of
TTimmy.Remove()due to the fact that other overloadedTTimmy.Remove()rely on this one, whether directly or indirectly. This one removes the array at offsetAIndexinTTimmy.MKeywordsListand inTTimmy.ReplyList. In other words, it removesMKeywordsList[AIndex]andReplyList[AIndex].
function Answer(TMessage: String)
- Source:
- Parameters:
TMessage(TQuestionin v1.0.0) [String]: End-user’s message to the bot - Return: String. Bot’s response to
TMessage. - Visibility: Public
- Availability: v1.0.0 to v1.2.0
- Description: Given the end-user’s message, returns the bot instance’s response to that message. The message is first pre-processed (like removing extra white-spaces or punctuations like ! or ?). Then, it is splitted into many words using the space character. The function will then iterate through
TTimmy.MKeywordsList, and compute the percentage of the keywords in each of the array inTTimmy.MKeywordsListto the user message’s splitted words. If the percentage is larger thenTTimmy.TPercent, a random reply in the corresponding array inTTimmy.ReplyListwill be returned toTTimmy.Answer(). In this case, we say that the bot has “understood” the end-user’s message. In the case that it could not understand,TTimmy.NoUdstdRepis returned.
Other functions provided by the unit & TStrArray
TStrArray
TStrArray is Array of Array of String. In Timmy, it is used instead of Array of Array of String to avoid type incompatible compile error.
function StrTrim(S: String)
- Source:
- Parameters:
S[String]: String to be processed. - Return: String. The processed string.
- Availability: v1.0.0 to v1.2.0 (In v1.0.0, it’s
StrProcessor()) - Description:
StrTrim()deletes extra white spaces in the stringS.
function StrSplit(S: String; delimiter: Char)
- Source:
- Parameters:
S[String]: String to be splitteddelimiter[Character]: Delimiter forS
- Return:
TStrArray, contains the strings ofSafter being splitted. - Availability: v1.0.0 to v1.2.0
- Description:
StrSplit()splits the stringSusingdelimiteras delimiter, and returns aTStrArrayholding the delimited strings.
function CompareStrArrays(ArrayA, ArrayB: TStrArray)
- Source:
- Parameters: Two
TStrArrays to be compared,ArrayAandArrayB. - Return: Boolean. True if
ArrayAis the same asArrayB, false otherwise. - Availability: v1.0.0 to v1.2.0
- Description: This function compares two
TStrArrays to see if they have the exact same elements. The order of elements in the arrays does matter.
License

Timmy is licensed under the GNU LGPL v3.