It was built using 2 tutorials:
- https://www.codementor.io/@garethdwyer/building-a-telegram-bot-using-python-part-1-goi5fncay
- https://github.com/python-engineer/pytorch-chatbot So thank you very much to @garethdwyer and Python Engineer! Everything that works here is credit to them, anything that doesn't is me.
This bot allows you to create your own transactional chatbot on Telegram using Python. You can talk to it with natural language and it will process the input, tag it into a category, and randomly pick a response in that category to reply with. It only replies to one message at a time and thus, does not remember context or past messages. This was useful for me as a personal bot to remind me of lessons I'd learnt, but may forget in different contexts. E.g. I feel unmotivated --> What are your values? How can you best express them right now?
This graph is in mermaid. If you don't have the browser plugin need to view it, copy-paste the below code to this site for viewing: https://mermaid-js.github.io/mermaid-live-editor/
graph LR
A[User sends message on Telegram] --> B[Get Json from Telegram API] --> C[Process Json to get latest message] --> F
D[Code NLP model]--> |Model stems inputs using nltk and creates bag of words stemmed words| E[Train model with intents.json database to match types of inputs with proposed outputs]
E --> F[Pass latest message from Json to trained model] --> G[Return response] --> H[Pass response back to telegram as URL] --> I[User receives repsonse as text message on Telegram]
Honestly not sure. This is my first time coding so I bulldozed through problems in the most inelegant way possible. Code from both tutorials have been frankensteined and arguments have been changed to make it work. I considered importing scripts into one another but I didn't understand the argument logic well enough to make it work so I formed my own.
- Refer here: https://github.com/python-engineer/pytorch-chatbot
- Setup environment by importing appropriate modules
- Customise intents.json
- Run train.py to create data.pth
- No need to run chat.py unlike the original tutorial. Data.pth is your trained model data and will be imported into chatbot_proper.py
4. Bot is setup and should work. Start it locally by running chatbot_proper.py in Terminal. Then, send your first input message on Telegram and you should get a response according to your intents.json.
The bot is working but is still only running on your local server, meaning you have to keep the script running on your laptop for it to work.
Next step, deploying on an online server like AWS so it will always be accessible. I haven't done that yet but you can check out how to online. Will update once I do it.
This has been a personal project starting from 0 knowledge of coding and python. Thus, the arguments are less than "pythonic" and I'm sure can be done better. Any suggested improvements would be appreciated. I haven't found something like this online so its available here.