mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-12-27 20:44:53 +00:00
263 lines
21 KiB
HTML
263 lines
21 KiB
HTML
<!DOCTYPE html>
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="generator" content="pandoc" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
|
<title>Bashbot README</title>
|
|
<style>
|
|
code{white-space: pre-wrap;}
|
|
span.smallcaps{font-variant: small-caps;}
|
|
span.underline{text-decoration: underline;}
|
|
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
|
</style>
|
|
<style>
|
|
a.sourceLine { display: inline-block; line-height: 1.25; }
|
|
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
|
|
a.sourceLine:empty { height: 1.2em; }
|
|
.sourceCode { overflow: visible; }
|
|
code.sourceCode { white-space: pre; position: relative; }
|
|
div.sourceCode { margin: 1em 0; }
|
|
pre.sourceCode { margin: 0; }
|
|
@media screen {
|
|
div.sourceCode { overflow: auto; }
|
|
}
|
|
@media print {
|
|
code.sourceCode { white-space: pre-wrap; }
|
|
a.sourceLine { text-indent: -1em; padding-left: 1em; }
|
|
}
|
|
pre.numberSource a.sourceLine
|
|
{ position: relative; left: -4em; }
|
|
pre.numberSource a.sourceLine::before
|
|
{ content: attr(title);
|
|
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
|
border: none; pointer-events: all; display: inline-block;
|
|
-webkit-touch-callout: none; -webkit-user-select: none;
|
|
-khtml-user-select: none; -moz-user-select: none;
|
|
-ms-user-select: none; user-select: none;
|
|
padding: 0 4px; width: 4em;
|
|
color: #aaaaaa;
|
|
}
|
|
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
|
|
div.sourceCode
|
|
{ }
|
|
@media screen {
|
|
a.sourceLine::before { text-decoration: underline; }
|
|
}
|
|
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
|
|
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
|
|
code span.at { color: #7d9029; } /* Attribute */
|
|
code span.bn { color: #40a070; } /* BaseN */
|
|
code span.bu { } /* BuiltIn */
|
|
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
|
|
code span.ch { color: #4070a0; } /* Char */
|
|
code span.cn { color: #880000; } /* Constant */
|
|
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
|
|
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
|
|
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
|
|
code span.dt { color: #902000; } /* DataType */
|
|
code span.dv { color: #40a070; } /* DecVal */
|
|
code span.er { color: #ff0000; font-weight: bold; } /* Error */
|
|
code span.ex { } /* Extension */
|
|
code span.fl { color: #40a070; } /* Float */
|
|
code span.fu { color: #06287e; } /* Function */
|
|
code span.im { } /* Import */
|
|
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
|
|
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
|
|
code span.op { color: #666666; } /* Operator */
|
|
code span.ot { color: #007020; } /* Other */
|
|
code span.pp { color: #bc7a00; } /* Preprocessor */
|
|
code span.sc { color: #4070a0; } /* SpecialChar */
|
|
code span.ss { color: #bb6688; } /* SpecialString */
|
|
code span.st { color: #4070a0; } /* String */
|
|
code span.va { color: #19177c; } /* Variable */
|
|
code span.vs { color: #4070a0; } /* VerbatimString */
|
|
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
|
|
</style>
|
|
<!--[if lt IE 9]>
|
|
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
|
|
<![endif]-->
|
|
</head>
|
|
<body>
|
|
<header id="title-block-header">
|
|
<h1 class="title">Bashbot README</h1>
|
|
</header>
|
|
<h2><img align="middle" src="https://raw.githubusercontent.com/odb/official-bash-logo/master/assets/Logos/Icons/PNG/64x64.png" >
|
|
Bashbot - A Telegram bot written in bash.
|
|
</h2>
|
|
Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadelwartz).
|
|
|
|
<p>Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1.</p>
|
|
<p>Released to the public domain wherever applicable. Elsewhere, consider it released under the <a href="http://www.wtfpl.net/txt/copying/">WTFPLv2</a>.</p>
|
|
<h2>Prerequisites</h2>
|
|
<p>Uses <a href="http://github.com/dominictarr/JSON.sh">JSON.sh</a> and the magic of (gnu) sed.</p>
|
|
<p>Even bashbot is written in bash, it depends on commands typically available in a Unix/Linux Environment. More concret on the common commands provided by recent versions of <a href="https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands">coreutils</a>, <a href="https://en.wikipedia.org/wiki/BusyBox#Commands">busybox</a> or <a href="https://landley.net/toybox/help.html">toybox</a>, see <a href="doc/7_develop.md#common-commands">Developer Notes</a></p>
|
|
<p><em>Note for MacOS and BSD Users:</em> As bashbot heavily uses modern bash and (gnu) grep/sed features, bashbot will not run without installing additional software, see <a href="doc/0_install.md">Install Bashbot</a></p>
|
|
<p>Bashbot <a href="https://github.com/topkecleon/telegram-bot-bash">Documentation</a> and <a href="https://github.com/topkecleon/telegram-bot-bash/releases">Downloads</a> are available on www.github.com</p>
|
|
<h2>Documentation</h2>
|
|
<ul>
|
|
<li><a href="https://core.telegram.org/bots">Introdution to Telegram Bots</a></li>
|
|
<li><a href="doc/0_install.md">Install Bashbot</a>
|
|
<ul>
|
|
<li>Install release</li>
|
|
<li>Install from githup</li>
|
|
<li>Update Bashbot</li>
|
|
<li>Notes on Updates</li>
|
|
</ul></li>
|
|
<li><a href="doc/1_firstbot.md">Get Bottoken from Botfather</a></li>
|
|
<li><a href="doc/2_usage.md">Getting Started</a>
|
|
<ul>
|
|
<li>Managing your Bot</li>
|
|
<li>Receive data</li>
|
|
<li>Send messages</li>
|
|
<li>Send files, locations, keyboards</li>
|
|
</ul></li>
|
|
<li><a href="doc/3_advanced.md">Advanced Features</a>
|
|
<ul>
|
|
<li>Access Control</li>
|
|
<li>Interactive Chats</li>
|
|
<li>Background Jobs</li>
|
|
<li>Inline queries</li>
|
|
<li>Send message errors</li>
|
|
</ul></li>
|
|
<li><a href="doc/4_expert.md">Expert Use</a>
|
|
<ul>
|
|
<li>Handling UTF-8 character sets</li>
|
|
<li>Run as other user or system service</li>
|
|
<li>Schedule bashbot from Cron</li>
|
|
<li>Use from CLI and Scripts</li>
|
|
<li>Customize Bashbot Environment</li>
|
|
</ul></li>
|
|
<li><a href="doc/5_practice.md">Best Practices</a>
|
|
<ul>
|
|
<li>Customize mycommands.sh</li>
|
|
<li>Overwrite/disable commands</li>
|
|
<li>Separate logic from commands</li>
|
|
<li>Test your Bot with shellcheck</li>
|
|
</ul></li>
|
|
<li><a href="doc/6_reference.md">Function Reference</a>
|
|
<ul>
|
|
<li>Sending Messages, Files, Keyboards</li>
|
|
<li>User Access Control</li>
|
|
<li>Inline Queries</li>
|
|
<li>jsshDB Bashbot key-value storage</li>
|
|
<li>Background and Interactive Jobs</li>
|
|
</ul></li>
|
|
<li><a href="doc/7_develop.md">Developer Notes</a>
|
|
<ul>
|
|
<li>Debug bashbot</li>
|
|
<li>Modules, addons, events</li>
|
|
<li>Setup your environment</li>
|
|
<li>Bashbot test suite</li>
|
|
</ul></li>
|
|
<li><a href="examples/README.md">Examples Dir</a></li>
|
|
</ul>
|
|
<h3>Your really first bashbot in a nutshell</h3>
|
|
<p>To install and run bashbot you need access to a linux/unix command line. If you don't know how to get access to a linux/unix/bsd like command line you should stop reading here :-(</p>
|
|
<p>In addition you need a <a href="https://telegram.org">Telegram client</a> and a mobile phone to <a href="https://telegramguide.com/create-a-telegram-account/">register an account</a>. If you don't want to register for Telegram you should stop reading here ;-)</p>
|
|
<p>After you're registered to Telegram send a message to <a href="https://telegram.me/botfather">@botfather</a>, <a href="doc/1_firstbot.md">create a new Telegram Bot token</a> and write it down. You need the token to install the bot.</p>
|
|
<p>Now open a linux/unix/bsd terminal and check if bash is installed: <code>which bash && echo "bash installed!"</code>. If you get an error message bash is not installed.</p>
|
|
<p>Create a new directory and change to it: <code>mkdir tbb; cd tbb</code> and download the latest '*.tar.gz' file from <a href="https://github.com/topkecleon/telegram-bot-bash/releases">https://github.com/topkecleon/telegram-bot-bash/releases</a>. This can be done with the commands:</p>
|
|
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb1-1" title="1"><span class="fu">wget</span> -q https://github.com/<span class="va">$(</span><span class="fu">wget</span> -q https://github.com/topkecleon/telegram-bot-bash/releases/latest -O - <span class="kw">|</span> <span class="fu">egrep</span> <span class="st">'/.*/.*/.*tar.gz'</span> -o<span class="va">)</span></a></code></pre></div>
|
|
<p>Extract the '*.tar.gz' file and change to bashbot directory: <code>tar -xzf *.tar.gz; cd telegram-bot-bash</code>, install bashbot: <code>./bashbot.sh init</code> and enter your bot token when asked. All other questions can be answered by hitting the <Return> key.</p>
|
|
<p>That's all, now you can start your bot with <code>./bashbot.sh start</code> and send him messages:</p>
|
|
<pre><code>/start
|
|
|
|
You are Botadmin
|
|
*Available commands*:
|
|
*• /start*: _Start bot and get this message_.
|
|
*• /help*: _Get this message_.
|
|
*• /info*: _Get shorter info message about this bot_....
|
|
|
|
/info
|
|
|
|
his is bashbot, the Telegram bot written entirely in bash.
|
|
It features background tasks and interactive chats, and can serve as an interface for CLI programs.
|
|
</code></pre>
|
|
<p>For more Information on how to install, customize and use your new bot, read the <a href="#Documentation">Documentation</a></p>
|
|
<h3>Log files</h3>
|
|
<p>Since version 0.96 bashbot log commands received/send and connection errors. If you start bashbot in debug mode bash stdout, stderr and all send/received telegram message are logged also.</p>
|
|
<p>To enable debug mode start bashbot with debug as third argument: <code>bashbot start debug</code></p>
|
|
<pre><code>├── logs
|
|
│ ├── BASHBOT.log # log what your bot is doing ...
|
|
│ ├── ERROR.log # connection errors from / to telegram API
|
|
│ │
|
|
│ ├── DEBUG.log # stdout/stderr of you bot (debug mode enabled)
|
|
│ └── MESSAGE.log # full text of all message send/received (debug mode enabled)
|
|
</code></pre>
|
|
<hr />
|
|
<h2>Security Considerations</h2>
|
|
<p>Running a Telegram Bot means it is connected to the public and you never know what's send to your Bot.</p>
|
|
<p>Bash scripts in general are not designed to be bullet proof, so consider this Bot as a proof of concept. Bash programmers often struggle with 'quoting hell' and globbing, see <a href="https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells">Implications of wrong quoting</a></p>
|
|
<p>Whenever you are processing input from from untrusted sources (messages, files, network) you must be as careful as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands.</p>
|
|
<p><strong>Note:</strong> Until v0.941 (mai/22/2020) telegram-bot-bash had a remote code execution (RCE) bug, pls update if you use an older version! see <a href="https://github.com/topkecleon/telegram-bot-bash/issues/125">Issue #125</a></p>
|
|
<p>One of the most powerful features of unix shells like bash is variable and command substitution using <code>${}</code> and <code>$()</code>, but as they are expanded in double quotes, this can lead to RCE and information disclosing bugs in complex scripts like bashbot even bash does much to avoid this. So it's more secure to escape or remove '$' in input from user, files or network.</p>
|
|
<p>A powerful tool to improve your scripts is <code>shellcheck</code>. You can <a href="https://www.shellcheck.net/">use it online</a> or <a href="https://github.com/koalaman/shellcheck#installing">install shellcheck locally</a>. Shellcheck is used extensive in bashbot development to enshure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a <a href="doc/7_develop.md">test suite</a> to check if important functionality is working as expected.</p>
|
|
<h3>use printf whenever possible</h3>
|
|
<p>If you're writing a script and it is taking external input (from the user as arguments, or file names from the file system...), you shouldn't use echo to display it. <a href="https://unix.stackexchange.com/a/6581">Use printf whenever possible</a></p>
|
|
<div class="sourceCode" id="cb4"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb4-1" title="1"> <span class="co"># very simple</span></a>
|
|
<a class="sourceLine" id="cb4-2" title="2"> <span class="bu">echo</span> <span class="st">"text with variables. PWD=</span><span class="va">$PWD</span><span class="st">"</span></a>
|
|
<a class="sourceLine" id="cb4-3" title="3"> <span class="bu">printf</span> <span class="st">'%s\n'</span> <span class="st">"text with variables. PWD=</span><span class="va">$PWD</span><span class="st">"</span></a>
|
|
<a class="sourceLine" id="cb4-4" title="4"> <span class="ex">-</span><span class="op">></span> text with variables. PWD=/home/xxx</a>
|
|
<a class="sourceLine" id="cb4-5" title="5"></a>
|
|
<a class="sourceLine" id="cb4-6" title="6"> <span class="co"># more advanced</span></a>
|
|
<a class="sourceLine" id="cb4-7" title="7"> <span class="va">FLOAT=</span><span class="st">"1.2346777892864"</span> <span class="va">INTEGER=</span><span class="st">"12345.123"</span></a>
|
|
<a class="sourceLine" id="cb4-8" title="8"> <span class="bu">echo</span> <span class="st">"text with variabeles. float=</span><span class="va">$FLOAT</span><span class="st">, integer=</span><span class="va">$INTEGER</span><span class="st">, PWD=</span><span class="va">$PWD</span><span class="st">"</span></a>
|
|
<a class="sourceLine" id="cb4-9" title="9"> <span class="ex">-</span><span class="op">></span>text with variables. float=1.2346777892864, integer=12345.123, PWD=/home/xxx</a>
|
|
<a class="sourceLine" id="cb4-10" title="10"></a>
|
|
<a class="sourceLine" id="cb4-11" title="11"> <span class="bu">printf</span> <span class="st">"text with variables. float=%.2f, integer=%d, PWD=%s\n"</span> <span class="st">""</span> <span class="st">"</span><span class="va">$INTEGER</span><span class="st">"</span> <span class="st">"</span><span class="va">$PWD</span><span class="st">"</span></a>
|
|
<a class="sourceLine" id="cb4-12" title="12"> <span class="ex">-</span><span class="op">></span>text with variables. float=1.23, integer=12345, PWD=/home/xxx</a></code></pre></div>
|
|
<h3>Do not use #!/usr/bin/env bash</h3>
|
|
<p><strong>We stay with /bin/bash shebang, because it's more save from security perspective.</strong></p>
|
|
<p>Using a fixed path to the system provided bash makes it harder for attackers or users to place alternative versions of bash and avoids using a possibly broken, mangled or compromised bash executable.</p>
|
|
<p>If you are a BSD / MacOS user or must to use an other bash location, see <a href="doc/0_install.md">Install Bashbot</a></p>
|
|
<h3>Run your Bot as a restricted user</h3>
|
|
<p><strong>I recommend to run your bot as a user, with almost no access rights.</strong> All files your Bot have write access to are in danger to be overwritten/deleted if your bot is hacked. For the same reason every file your Bot can read is in danger to be disclosed. Restict your Bots access rights to the absolute minimum.</p>
|
|
<p><strong>Never run your Bot as root, this is the most dangerous you can do!</strong> Usually the user 'nobody' has almost no rights on Unix/Linux systems. See <a href="doc/4_expert.md">Expert use</a> on how to run your Bot as an other user.</p>
|
|
<h3>Secure your Bot installation</h3>
|
|
<p><strong>Your Bot configuration must no be readable from other users.</strong> Everyone who can read your Bots token can act as your Bot and has access to all chats your Bot is in!</p>
|
|
<p>Everyone with read access to your Bot files can extract your Bots data. Especially your Bot Token in <code>token</code> must be protected against other users. No one except you must have write access to the Bot files. The Bot must be restricted to have write access to <code>count</code> and <code>tmp-bot-bash</code> only, all other files must be write protected.</p>
|
|
<p>To set access rights for your bashbot installation to a reasonable default run <code>sudo ./bashbot.sh init</code> after every update or change to your installation directory.</p>
|
|
<h2>FAQ</h2>
|
|
<h3>Is this Bot insecure?</h3>
|
|
<p>Bashbot is not more (in)secure as any other Bot written in any other language, we have done our best to make it as secure as possible. But YOU are responsible for the bot commands you wrote and you should know about the risks ...</p>
|
|
<p><strong>Note:</strong> Until v0.941 (mai/22/2020) telegram-bot-bash has a remote code execution bug, pls update if you use an older version!</p>
|
|
<h3>Why Bash and not the much better xyz?</h3>
|
|
<p>Well, that's a damn good question ... may be because I'm an Unix/Linux admin from stone age. Nevertheless there are more reasons from my side:</p>
|
|
<ul>
|
|
<li>bashbot will run everywhere where bash and (gnu) sed is available, from embedded linux to mainframe</li>
|
|
<li>easy to integrate with other shell script, e.g. for sending system message / health status</li>
|
|
<li>no need to install or learn a new programming language, library or framework</li>
|
|
<li>no database, not event driven, not OO ...</li>
|
|
</ul>
|
|
<h3>Can I have the single bashbot.sh file back?</h3>
|
|
<p>At the beginning bashbot was simply the file <code>bashbot.sh</code> you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more.</p>
|
|
<p>Hey no Problem, if you are finished with your cool bot run <code>dev/make-standalone.sh</code> to create a stripped down Version of your bot containing only 'bashbot.sh' and 'commands.sh'! For more information see <a href="doc/7_develop.md">Create a stripped down Version of your Bot</a></p>
|
|
<h3>Can I send messages from CLI and scripts?</h3>
|
|
<p>Of course, you can send messages from CLI and scripts, simply install bashbot as <a href="#Your-really-first-bashbot-in-a-nutshell">described here</a>, send the message '/start' to set yourself as botadmin and stop the bot with <code>./bashbot.sh kill</code>.</p>
|
|
<p>Run the following commands in your bash shell or script while you are in the installation directory:</p>
|
|
<div class="sourceCode" id="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb5-1" title="1"><span class="co"># prepare bash / script to send commands</span></a>
|
|
<a class="sourceLine" id="cb5-2" title="2"><span class="bu">export</span> <span class="va">BASHBOT_HOME=</span><span class="st">"</span><span class="va">$(</span><span class="bu">pwd</span><span class="va">)</span><span class="st">"</span></a>
|
|
<a class="sourceLine" id="cb5-3" title="3"><span class="bu">source</span> ./bashbot.sh source</a>
|
|
<a class="sourceLine" id="cb5-4" title="4"></a>
|
|
<a class="sourceLine" id="cb5-5" title="5"><span class="co"># send me a test message</span></a>
|
|
<a class="sourceLine" id="cb5-6" title="6"><span class="ex">send_message</span> <span class="st">"</span><span class="va">$(</span><span class="fu">cat</span> <span class="st">"</span><span class="va">$BOTADMIN</span><span class="st">"</span><span class="va">)</span><span class="st">"</span> <span class="st">"test"</span></a>
|
|
<a class="sourceLine" id="cb5-7" title="7"></a>
|
|
<a class="sourceLine" id="cb5-8" title="8"><span class="co"># send me output of a system command</span></a>
|
|
<a class="sourceLine" id="cb5-9" title="9"><span class="ex">send_message</span> <span class="st">"</span><span class="op">$(<</span><span class="st">"</span><span class="va">$BOTADMIN</span><span class="st">"</span><span class="op">)</span><span class="st">"</span> <span class="st">"</span><span class="va">$(</span><span class="fu">df</span> -h<span class="va">)</span><span class="st">"</span></a></code></pre></div>
|
|
<p>For more information see <a href="doc/8_custom.md">Expert Use</a></p>
|
|
<h3>Blocked by telegram?</h3>
|
|
<p>This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a tor proxy on your server you may uncomment the <code>BASHBOT_CURL_ARGS</code> line in 'mycommands.sh'</p>
|
|
<p>You can test if younare blockeds by running curl or wget manually:</p>
|
|
<div class="sourceCode" id="cb6"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb6-1" title="1"><span class="ex">curl</span> -m 10 https://api.telegram.org/bot</a>
|
|
<a class="sourceLine" id="cb6-2" title="2"><span class="co">#curl: (28) Connection timed out after 10001 milliseconds</span></a>
|
|
<a class="sourceLine" id="cb6-3" title="3"></a>
|
|
<a class="sourceLine" id="cb6-4" title="4"><span class="fu">wget</span> -t 1 -T 10 https://api.telegram.org/bot</a>
|
|
<a class="sourceLine" id="cb6-5" title="5"><span class="co">#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.</span></a></code></pre></div>
|
|
<p>@Gnadelwartz</p>
|
|
<h2>That's it!</h2>
|
|
<p>If you feel that there's something missing or if you found a bug, feel free to submit a pull request!</p>
|
|
<h4>$$VERSION$$ v0.98-pre-3-g0f24eb8</h4>
|
|
</body>
|
|
</html>
|