2018-09-28 22:09:48 +02:00
* [Bot Token Dont's ](#bot-token-donts )
* [Using Enviroment Variables ](#using-environment-variables )
* [Setting Enviroment Variables ](#setting-environment-variables )
* [Accessing Enviroment Variables ](#accessing-enviroment-variables )
* [Using Command Line Arguments ](#using-command-line-arguments )
2018-09-28 22:11:33 +02:00
# <a id="bot-token-donts"></a> Bot Token Dont's ##
2018-09-28 22:09:48 +02:00
* Tokens should not be hardcoded into the bot code
* Tokens should never be published
* Tokens should not be pushed into Repositorys
2018-09-28 22:11:33 +02:00
# <a id="using-environment-variables"></a> Using Environment Variables ###
2018-09-28 22:09:48 +02:00
One convenient way to inject your bot token into the application is by using Environment Variables. Environment Variables are Values that are set in the Environment the Bot is running.
Those Values are not defined in the Application and therefore are not visible in the code.
2018-09-28 22:11:33 +02:00
## <a id="setting-environment-variables"></a> Setting Environment Variables ###
2018-09-28 22:09:48 +02:00
2018-09-28 22:14:00 +02:00
### Windows
2018-09-28 22:09:48 +02:00
Enviroment Variables in Windows can be set using the Console (CMD) using
```batchfile
SETX [VARIABLE_NAME] [YOUR_BOT_TOKEN]
```
It can also be set using the Windows GUI
* From the desktop, right click the Computer icon.
* Choose Properties from the context menu.
* Click the Advanced system settings link.
* Click Environment Variables...
* In the 'User Variables for X' click New and enter a Name and your Token as the Value
2018-09-28 22:14:00 +02:00
### Linux & Mac
2018-09-28 22:09:48 +02:00
* Open the '~/.bash_profile' File
* Append the following to it:
```bash
export VARIABLE_NAME = {YOUR_BOT_TOKEN}
```
* Save the file
* Either reboot your system or run the command above in your terminal
2018-09-28 22:14:00 +02:00
### IntelliJ
2018-09-28 22:09:48 +02:00
* Go to Run->Edit Configuratuions...
* Navigate to your Java Run Configuration
* Under Enviroment->Enviroment Variables click the Folder Icon
* Click the Plus Icon to add a new Variable
* Enter a Name and your Token as the Value
2018-09-28 22:11:33 +02:00
###Heroku Cloud
2018-09-28 22:09:48 +02:00
* Naviage to your App
* In the Settings Tab under Config Vars, click "Reveal Config Vars"
* Enter a Name and your Token as the Value
* Click the "Add" button
2018-09-28 22:14:00 +02:00
## <a id="accessing-enviroment-variables"></a> Accessing Enviroment Variables ##
2018-09-28 22:09:48 +02:00
2018-09-28 22:14:00 +02:00
### Java
2018-09-28 22:09:48 +02:00
You can access the Enviroment Variables by using System.getEnv()
```java
String BOT_TOKEN = System.getenv("VARIABLE_NAME");
```
2018-09-28 22:14:00 +02:00
### Spring
2018-09-28 22:09:48 +02:00
In Spring the @Value annotation allows you to inject the Value into your class
```java
public class Bot extends TelegramLongPollingBot {
public Bot(@Value("${VARIABLE_NAME") String botToken) {
this.botToken = botToken;
}
}
```
2018-09-28 22:11:33 +02:00
# <a id="sing-command-line-arguments"></a> Using Command Line Arguments
2018-09-28 22:09:48 +02:00
An easier but not Recommended way of injecting the Bottoken is by utilizing Command Line Arguments when starting the Application
In this case your main Method is responsible for taking in the Token
```java
public static void main(String[] args) {
String botToken = args[0];
}
```
You now have to call your jar by using
```
java -jar myBot.jar [BOT_TOKEN]
```