Visibility of files and global variables using pyTelegramBot - python-3.x

So i've been working on a chatbot on telegram using python (pyTelegramBot API), and started to look into saving users data in a database (.db) file.
My question can be devided into two questions:
While asking information from the user - the bot stores the input data from the user in a global dictionary that stores the user data. Now as far as I know - using a global variable is bad practice, but that was the best way I found to deal with storing the data across different functions.
I also read that using a User class will be better - but I fail to understand how a global object would be better. Maybe I'm missing something very obvious here but this is really stopping my project from advancing
About using .db file (SQLite3) - I guess that if someone would want to gain access to my db file, he would need to go on the file server and open the file itself. is there any way I can protect this file from being opened by anyone but me and my bot?
Thank you!

Related

Nodejs config which is subject to change

I was wondering what the best way is to save config values in nodejs/express which are subject to change. I'm not talking about database credentials that you store inside your environment variables. But say a game where the config values are changeable within the admin panel. A config for example could be how often in ms a player receives a random reward. I could store this directly into the code (but I'd have to alter my code everytime I would like to change this and this cant be changed from within an admin panel), have some different file that has an object with these configs or save it into a mysql database (I would need to query the db everytime I need a config though).
I'm not really sure what the best way is to do this. Basically I want a way to store flexible configs within my nodejs application that are subject to change.
Thanks in advance.

Where to save data in express.js app other than database?

I am showing a link on a page and I want to change that link, by using a form.
where should I save that link and retrieve to render the page other that a database?
*I think involving database for such a small task is not performance efficient.
what I can do is save it in a global variable as a string, so that I can access it and change it.
But is it a good practice to use global variable for such task in production?
OK, now that we know that this is a change that you want to affect all users, then a global or preferably a module-level variable or a property on a module-shared object would be fine.
If you want this change to be persistent and survive a server restart, then you would need to write the change to disk somewhere and have code that reads that setting back in from disk when your server restarts. A simple way to do that might be to read/write it to a file in the JSON format. That gives you simple extensibility to also include other settings in that file.
Your admin form can then just update this variable and trigger a save to disk of all the current settings.
I believe which approach you want to implement really depends on the scenario e.g. how frequently will the link be changed, how frequently will it be read etc. A couple of suggestions:
If different user wants to see and update the same link without affecting others, you can use on client side stored cookies. Then you won't need a db but each user will manage their own link that no one else can access.
You could use a file e.g. json or simple text file and use built in fs to read and write into that file. However in that case you would want to use sync operations to avoid concurrency issues e.g. const contents = fs.readFileSync('storage.txt', 'utf8');
Of course you could store data in a string too, however if server was to go down, the link would not persist.

How to deal with data which is very little that I don't want to save in database?

The scenario is:
I am working on a express.js app with mongoDB and EJS.
There is a URL/ link on a page which I want to change, let's say using
a form.
I am already using user model to retrieve and update user data.
What I can do is save that link in a collection in mongoDB i.e. create
a model.
But, I think it's not good to create a model for just a link and get
it to render on EJS.
What should I do any suggestions? Any tricks?
*No need to read if you already know what I should do.
I tried something which is not a good practice and sometimes causing big issues.
I have added a JSON file in public directory to serve.
I am reading this file to get the link on client side.
when I want to change the link, I submit new link using a form and on server
side overwrite the content of that file (JSON file present in public directory).
so next time that file will be served with changed content.
I tried overwriting the contents of that file(using "fs") synchronously as well as asynchronously but because that may be sometimes web page stucks for a second, but it is not crashing the app.
may be this was silly, please suggest if you know anything i should do.
*NOTE: Sorry, if this question is inappropriate for StackOverflow. But I am struggeling to find any solution.

Best practice for storing the chatbot's answers

My issue is that I don't know what is considered a good practice for storing the chatbot's answers. I do not wish to have the answer in plain text inside the bot.dialog() in code like this:
bot.dialog('Greeting', session => {
session.endDialog('Hello, I'm a bot.');
});
... But instead fetch the answers from a database or a local file.
Since the bot is supposed to run in Azure, I've been looking at Azure Cosmos DB with MongoDB, but are there better alternatives? Given the simplicity of the database, would be a better practice to store the bot's answer in a local JSON file and store it in the same catalog as my app.js file?
The bot is supposed to be an FAQ bot, so the DB/file would mostly consist of answers that the bot is going to give for the different types of dialogs. There will most likely be no relation between the data.

Storing files on a datastore and getting back their location to store for later retrieval

I will need to store photo's on a datastore, store the location in Mongo so I can retrieve it later to display. What is a good way to go about doing this? If I just google I end up getting information on accessing the files in node itself.....
At this point I am not sure what datastore I will use, for now just on another server I have, Ubuntu Server.
I made something like what you need. I had to store photos on s3 and store the path on mongo. If you use amanzon too, they provide a sdk, that contains all possible functions for interactions with the cloud. So you'll need a way to work with asynchronous tasks when store the data in the cloud, I used async module to manage the functions. I hope I gave you some direction where go to. I can't help you code, once I don't know your business rules, good lucky

Resources