telegram-bot-bash/doc/4_expert.md

109 lines
4.8 KiB
Markdown
Raw Normal View History

2019-04-16 18:43:54 +00:00
#### [Home](../README.md)
2019-04-09 10:40:13 +00:00
## Expert Use
### Handling UTF-8 character sets
UTF-8 is a variable length encoding of Unicode. UTF-8 is recommended as the default encoding in JSON, XML and HTML, also Telegram make use of it.
The first 128 characters are regular ASCII, so it's a superset of and compatible with ASCII environments. The next 1,920 characters need
two bytes for encoding and covers almost all ```Latin``` alphabets, also ```Greek```, ```Cyrillic```,
2019-04-16 14:45:26 +00:00
```Hebrew```, ```Arabic``` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more details.
2019-04-09 10:40:13 +00:00
#### Setting up your Environment
2019-04-16 11:29:49 +00:00
In general ```bash``` and ```GNU``` utitities are UTF-8 aware if you to setup your environment
2019-04-09 10:29:25 +00:00
and your scripts accordingly:
1. Your Terminal and Editor must support UTF-8:
Set Terminal and Editor locale to UTF-8, eg. in ```Settings/Configuration``` select UTF-8 (Unicode) as Charset.
2. Set ```Shell``` environment to UTF-8 in your ```.profile``` and your scripts. The usual settings are:
```bash
export 'LC_ALL=C.UTF-8'
export 'LANG=C.UTF-8'
export 'LANGUAGE=C.UTF-8'
```
If you use other languages, eg. german or US english, change the shell settings to:
```bash
export 'LC_ALL=de_DE.UTF-8'
export 'LANG=de_DE.UTF-8'
export 'LANGUAGE=de_DE.UTF-8'
```
```bash
export 'LC_ALL=en_US.UTF-8'
export 'LANG=de_en_US.UTF-8'
export 'LANGUAGE=den_US.UTF-8'
```
3. make shure your bot scripts use the correct settings, eg. include the lines above at the beginning of your scripts
To display all availible locales on your system run ```locale -a | more```. [Gentoo Wiki](https://wiki.gentoo.org/wiki/UTF-8)
2019-04-18 08:35:18 +00:00
#### Bashbot UTF-8 Support
2019-04-16 14:45:26 +00:00
Bashbot handles all messages transparently, regardless of the charset in use. The only exception is when converting from JSON data to strings.
2019-04-16 11:29:49 +00:00
2019-04-16 14:45:26 +00:00
Telegram use JSON to send / recieve data. JSON encodes strings as follow: Characters not ASCII *(>127)* are escaped as sequences of ```\uxxxx``` to be regular ASCII. In addition multibyte characters, *e.g. Emoticons or Arabic characters*, are send in double byte UTF-16 notation.
2019-04-14 15:50:10 +00:00
The Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: ``` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ```
2019-04-09 10:29:25 +00:00
2019-04-16 14:45:26 +00:00
**This "mixed" JSON encoding needs special handling and can not decoded from** ```echo -e``` or ```printf '%s\\n'```
2019-04-09 10:29:25 +00:00
2019-04-18 08:35:18 +00:00
Most complete support for decoding of multibyte characters can only be provided if python is installed on your system.
**Without phyton bashbot falls back to an internal, pure bash implementation which may not work for some corner cases**.
2019-04-09 10:29:25 +00:00
### Run as other user or system service
2019-04-09 10:40:13 +00:00
Bashbot is desingned to run manually by the user who installed it. Nevertheless it's possible to run it by an other user-ID, as a system service or sceduled from cron. This is onyl recommended for experiend linux users.
2019-04-09 10:29:25 +00:00
Setup the environment for the user you want to run bashbot and enter desired username, e.g. nobody :
```bash
sudo ./bashbot.sh init
```
Edit the file ```bashbot.rc``` and edit the following lines to fit your configuration:
```bash
#######################
# Configuration Section
# edit the next line to fit the user you want to run bashbot, e.g. nobody:
runas="nobody"
# uncomment one of the following lines
# runcmd="su $runas -s /bin/bash -c " # runasuser with su
# runcmd="runuser $runas -s /bin/bash -c " # runasuser with runuser
# edit the values of the following lines to fit your config:
start="/usr/local/telegram-bot-bash/bashbot.sh" # location of your bashbot.sh script
name='' # your bot name as given to botfather, e.g. mysomething_bot
# END Configuration
#######################
```
2019-04-16 14:45:26 +00:00
From now on use 'bashbot.rc' to manage your bot:
2019-04-09 10:29:25 +00:00
```bash
sudo ./bashbot.rc start
```
Type ```ps -ef | grep bashbot``` to verify your Bot is running as the desired user.
2019-04-16 14:45:26 +00:00
If your Bot is started by 'bashbot.rc', you must use 'bashbot.rc' also to manage your Bot! The following commands are availible:
2019-04-09 10:29:25 +00:00
```bash
sudo ./bashbot.rc start
sudo ./bashbot.rc stop
sudo ./bashbot.rc status
sudo ./bashbot.rc suspendback
sudo ./bashbot.rc resumeback
sudo ./bashbot.rc killback
```
To change back the environment to your user-ID run ```sudo ./bashbot.sh init``` again and enter your user name.
To use bashbot as a system servive include a working ```bashbot.rc``` in your init system (systemd, /etc/init.d).
### Scedule bashbot from Cron
2019-04-19 09:28:12 +00:00
An example crontab is provided in ```examples/bashbot.cron```.
2019-04-09 10:29:25 +00:00
- If you are running bashbot with your user-ID, copy the examples lines to your crontab and remove username ```nobody```.
2019-04-19 09:28:12 +00:00
- if you run bashbot as an other user or a system service edit ```examples/bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot```
2019-04-09 10:29:25 +00:00
2019-04-16 14:45:26 +00:00
#### [Prev Expert Use](4_expert.md)
#### [Next Best Practice](5_practice.md)
2019-04-09 11:06:18 +00:00
2019-04-23 13:48:58 +00:00
#### $$VERSION$$ v0.70-dev2-10-gfa9e879
2019-04-09 10:29:25 +00:00