To keep the different parts of this series separate, copy the echobot.py to a new file called todobot.py, and work with the new file instead. We'll be starting from where we left off in Part 1 of this tutorial, where we had a script called echobot.py which echoed back every message that was sent to it. Let's modify the Chatbot code to use this new functionality. This DBHelper class that we've just created can be used by our Chatbot to do everything it needs to add, remove, and display items. We use a list comprehension to take the first element of each item, as SQLite will always return data in a tuple format, even when there is only one column, so in this example each item we retrieve from the database will be similar to ("buy groceries", ) (a tuple) which the list comprehension converts to "buy groceries" (a simple string). get_items() returns a list of all the items in our database.delete_item() takes the text for an item and removes it from the database.add_item() takes the text for the item and inserts it into our database table.This table has one column (called description) setup() creates a new table called items in our database._init_() takes a database name (by default store our data in a file called todo.sqlite) and creates a database connection.Stmt = "SELECT description FROM items" return for x in (stmt)] Stmt = "DELETE FROM items WHERE description = (?)" Stmt = "INSERT INTO items (description) VALUES (?)" Stmt = "CREATE TABLE IF NOT EXISTS items (description text)" The dbhelper.py codeĬreate a new file called dbhelper.py in the same directory as your Chatbot script, and add the following code: import sqlite3Ĭlass DBHelper: def _init_ (self, dbname= "todo.sqlite"): All of this functionality should be contained in our dbhelper.py script, as all of it interacts with the database directly. Finally, it'll need to be able to create the database table, in case we need to set up a new database (e.g. Furthermore, it'll need to be able to get all the items to display them to the user. Our Chatbot will need to be able to add a new item to the database of items that it is remembering for the user, and it will also need to delete an item once the user has marked that item as done. We'll see later on that this is a drastic oversimplification of the problem, but it'll be good enough to get us started. For now, we'll model each item by simply storing its description in a table in a database. We'll follow an iterative approach to building this bot, meaning that at each step, we'll make the problem as simple as possible and then solve that before iterating on the project to build a more nuanced solution.Ī To Do list consists of a number of items with text descriptions such as "Buy groceries", or "Finish writing Chatbot tutorial". Whenever you need to build a database, the first step is to think carefully about exactly how you're going to represent the problem. Any code that touches the database will be confined to a new script, dbhelper.py and we'll import this script into the main Bot script. We'll follow good coding practice and keep our database-related code separate from the main logic of our Bot. Adding the concept of ownership to items, so that multiple users can use our bot.īy the end of this tutorial, you'll know how to use some of the more advanced features of the Telegram API (specifically, the custom keyboard feature), and how to add a SQLite database to your Chatbots.Adding a custom Telegram Keyboard, so that our user can select options instead of typing out an entire message.Building a Database Helper script to cover adding data to a SQLite database and retrieving it again.In this tutorial we'll cover the following: But if you haven't, you'll probably be able to keep up anyway. Ideally, you should already be familiar with concepts such as Tables, Rows, Columns, and have written at least a basic SELECT * FROM SQL statement. In addition to the basic Python knowledge required for Part 1, it would be good if you had some familiarity with relational databases for this tutorial. We'll build a simple To Do list that allows users to add new items or delete existing ones. In this tutorial, we'll add a SQLite database backend to our bot and allow it to remember information for specific users indefinitely. A major limit of our previous bot though was that it couldn't store any information. The Bot we built is a good foundation for a wide range of possible bots, as we could take input, process it, and return a result - the foundation of classical computing. Our Bot wasn't too smart, and simply echoed anything sent to it back to the user. In Part 1 of this tutorial, we built a basic Telegram Chatbot from scratch using Python.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |