Merge branch 'develop' into master

This commit is contained in:
Kay Marquardt 2020-12-31 23:05:52 +01:00 committed by GitHub
commit 7f032b16e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 830 additions and 914 deletions

2
.gitignore vendored
View File

@ -11,7 +11,7 @@
*.log
*.swp
*.swo
/log/*
/logs/
/JSON.sh/
/data-bot-bash/
/DIST/

View File

@ -287,15 +287,32 @@ Written by Drew (@topkecleon) and Kay M (@gnadelwartz).
<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 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 terminal and check if bash is installed:</p>
<pre><code>which bash &amp;&amp; echo &quot;bash seems available...&quot;</code></pre>
<p>Create a new directory, 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="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">wget</span> <span class="at">-q</span> https://github.com/<span class="va">$(</span><span class="fu">wget</span> <span class="at">-q</span> https://github.com/topkecleon/telegram-bot-bash/releases/latest <span class="at">-O</span> <span class="at">-</span> <span class="kw">|</span> <span class="fu">egrep</span> <span class="st">&#39;/.*/.*/.*tar.gz&#39;</span> <span class="at">-o</span><span class="va">)</span></span></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 &lt;Return&gt; key.</p>
<p>That's all, now you can start your bot with <code>./bashbot.sh start</code> and send him messages:</p>
<p>To install and run bashbot you need access to a linux/unix command line with bash, a <a href="https://telegram.org">Telegram client</a> and a mobile phone <a href="https://telegramguide.com/create-a-telegram-account/">with a Telegram account</a>.</p>
<p>First you need to <a href="doc/1_firstbot.md">create a new Telegram Bot token</a> for your bot and write it down.</p>
<p>Now open a linux/unix terminal with bash, create a new directory, change to it and install telegram-bot-bash:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># create bot dir</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">mkdir</span> mybot</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="bu">cd</span> mybot</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co"># download latest release with wget or from https://github.com/topkecleon/telegram-bot-bash/releases/latest</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="fu">wget</span> <span class="st">&quot;https://github.com/</span><span class="va">$(</span><span class="fu">wget</span> <span class="at">-q</span> <span class="st">&quot;https://github.com/topkecleon/telegram-bot-bash/releases/latest&quot;</span> <span class="at">-O</span> <span class="at">-</span> <span class="kw">|</span> <span class="fu">egrep</span> <span class="st">&#39;/.*/download/.*/.*tar.gz&#39;</span> <span class="at">-o</span><span class="va">)</span><span class="st">&quot;</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Extract the tar archive and go into bot dir</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="fu">tar</span> <span class="at">-xzf</span> <span class="pp">*</span>.tar.gz</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="bu">cd</span> telegram-bot-bash</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="co"># initialize your bot</span></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="co"># Enter your bot token when asked, all other questions can be answered by hitting the \&lt;Return\&gt; key.</span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a><span class="ex">./bashbot.sh</span> init</span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a><span class="co"># Now start your bot</span></span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a><span class="ex">./bashbot.sh</span> start</span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a><span class="ex">Bottoken</span> is valid ...</span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a><span class="ex">Bot</span> Name: yourbotname_bot</span>
<span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a><span class="ex">Session</span> Name: yourbotname_bot-startbot</span>
<span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a><span class="ex">Bot</span> started successfully.</span></code></pre></div>
<p>Now open the Telegram App on your mobile phone and start a chatting with your bot (<em>your bot's username is shown after 'Bot Name:'</em>):</p>
<pre><code>/start
You are Botadmin
@ -310,7 +327,7 @@ This 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>Bashbot actions are logged to <code>BASHBOT.log</code>, Telegram send/receive errors are logged to <code>ERROR.log</code>. Start bashbot in debug mode to get all messages send to / received from Telegram and error messages of bash commands 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 ...
@ -322,36 +339,18 @@ It features background tasks and interactive chats, and can serve as an interfac
<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 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> Up to version v0.941 (mai/22/2020) telegram-bot-bash had a remote code execution (RCE) bug, please 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 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. So it's more secure to escape or remove '$' in input from user, files or network.</p>
<p>Whenever you are processing input 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 remove unused scripts and examples from your Bot, e.g. everything in <code>example/</code> and disable/remove all not needed bot commands.</p>
<p>It's important to escape or remove <code>$</code> in input from user, files or network (<em>as bashbot does</em>) One of the powerful features of unix shells are variable and command substitution using <code>${}</code> and<code>$()</code>, this can lead to remote code execution (RCE) or remote information disclosure (RID) bugs if unescaped <code>$</code> is included in untrusted input, e.g. <code>$$</code> or <code>$(rm -rf /*)</code></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 extensively in bashbot development to ensure 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 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="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a> <span class="co"># very simple</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">echo</span> <span class="st">&quot;text with variables. PWD=</span><span class="va">$PWD</span><span class="st">&quot;</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="bu">printf</span> <span class="st">&#39;%s\n&#39;</span> <span class="st">&quot;text with variables. PWD=</span><span class="va">$PWD</span><span class="st">&quot;</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">printf</span> <span class="st">&#39;text with variables. PWD=%s\n&#39;</span> <span class="st">&quot;</span><span class="va">$PWD</span><span class="st">&quot;</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="ex">-</span><span class="op">&gt;</span> text with variables. PWD=/home/xxx</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># more advanced</span></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="va">FLOAT=</span><span class="st">&quot;1.2346777892864&quot;</span> <span class="va">INTEGER=</span><span class="st">&quot;12345.123&quot;</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="bu">echo</span> <span class="st">&quot;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">&quot;</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="ex">-</span><span class="op">&gt;</span> float=1.2346777892864, integer=12345.123, PWD=/home/xxx</span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> <span class="bu">printf</span> <span class="st">&quot;text with variables. float=%.2f, integer=%d, PWD=%s\n&quot;</span> <span class="st">&quot;</span><span class="va">$FLOAT</span><span class="st">&quot;</span> <span class="st">&quot;</span><span class="va">$INTEGER</span><span class="st">&quot;</span> <span class="st">&quot;</span><span class="va">$PWD</span><span class="st">&quot;</span></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a> <span class="ex">-</span><span class="op">&gt;</span> float=1.23, integer=12345, PWD=/home/xxx</span></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>Use of 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. Restrict 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 is able to act as your Bot and has access to all chats the Bot is in!</p>
<p>Everyone with read access to your Bot files can extract your Bots data. Especially your Bot config in <code>config.jssh</code> must be protected against other users. No one except you should have write access to the Bot files. The Bot should be restricted to have write access to <code>count.jssh</code> and <code>data-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>
<p>Everyone with read access to your Bot files can extract your Bots data. Especially your Bot config in<code>config.jssh</code> must be protected against other users. No one except you should have write access to the Bot files. The Bot should be restricted to have write access to<code>count.jssh</code> and <code>data-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 Bot written in an 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>
@ -365,45 +364,32 @@ It features background tasks and interactive chats, and can serve as an interfac
<li>no database, not event driven, not object oriented ...</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>
<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 stop</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="cb6"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># prepare bash / script to send commands</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">BASHBOT_HOME=</span><span class="st">&quot;</span><span class="va">$(</span><span class="bu">pwd</span><span class="va">)</span><span class="st">&quot;</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="bu">source</span> ./bashbot.sh source</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="co"># send me a test message</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="ex">send_message</span> <span class="st">&quot;</span><span class="va">$(</span><span class="ex">getConfigKey</span> <span class="st">&quot;botadmin&quot;</span><span class="va">)</span><span class="st">&quot;</span> <span class="st">&quot;test&quot;</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="co"># send me output of a system command</span></span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a><span class="ex">send_message</span> <span class="st">&quot;</span><span class="va">$(</span><span class="ex">getConfigKey</span> <span class="st">&quot;botadmin&quot;</span><span class="va">)</span><span class="st">&quot;</span> <span class="st">&quot;</span><span class="va">$(</span><span class="fu">df</span> <span class="at">-h</span><span class="va">)</span><span class="st">&quot;</span></span></code></pre></div>
<p>For more information see <a href="doc/8_custom.md">Expert Use</a></p>
<p>Of course, you can send messages from command line 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 then stop the bot with <code>./bashbot.sh stop</code>.</p>
<p>Bashbot provides some ready to use scripts for sending messages from command line in <code>bin/</code> dir, e.g. <code>send_message.sh</code>.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="ex">bin/send_message.sh</span> BOTADMIN <span class="st">&quot;This is my first message send from CLI&quot;</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="ex">bin/send_message.sh</span> <span class="at">--help</span></span></code></pre></div>
<p>You can also source bashbot for use in your scripts, 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 or wrong requests are sent to api.telegram.org, e.g. using a invalid token or not existing API calls. If the block stay for longer time you can ask telegram service to unblock your IP-Adress.</p>
<p>You can check with curl or wget if you are blocked by Telegram:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="ex">curl</span> <span class="at">-m</span> 10 https://api.telegram.org/bot</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="co">#curl: (28) Connection timed out after 10001 milliseconds</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="fu">wget</span> <span class="at">-t</span> 1 <span class="at">-T</span> 10 https://api.telegram.org/bot</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="co">#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.</span></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a><span class="ex">nc</span> <span class="at">-w</span> 2 api.telegram.org 443 <span class="kw">||</span> <span class="bu">echo</span> <span class="st">&quot;your IP seems blocked by telegram&quot;</span></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a><span class="co">#your IP seems blocked by telegram</span></span></code></pre></div>
<p>Since Version 0.96 bashbot offers the option to recover from broken connections (aka blocked). Therefore you can provide a function named <code>bashbotBlockRecover()</code> in <code>mycommands.sh</code>. If the function exists it is called every time when a broken connection is detected.</p>
<p>Possible actions are: Check if network is working, change IP-Adress or simply wait some time.</p>
<p>If everything seems OK return 0 for retry or any non 0 value to give up.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># called when bashbot sedn command failed because we can not connect to telegram</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="co"># return 0 to retry, return non 0 to give up</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="fu">bashbotBlockRecover()</span> <span class="kw">{</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># place your commands to unblock here, e.g. change IP-Adess or simply wait</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">sleep</span> 60 <span class="kw">&amp;&amp;</span> <span class="cf">return</span> <span class="dv">0</span> <span class="co"># may be temporary</span></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="dv">1</span> </span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a> <span class="er">}</span></span></code></pre></div>
<div class="sourceCode" id="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ex">curl</span> <span class="at">-m</span> 10 https://api.telegram.org/bot</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="co">#curl: (28) Connection timed out after 10001 milliseconds</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="fu">wget</span> <span class="at">-t</span> 1 <span class="at">-T</span> 10 https://api.telegram.org/bot</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="co">#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a><span class="ex">nc</span> <span class="at">-w</span> 2 api.telegram.org 443 <span class="kw">||</span> <span class="bu">echo</span> <span class="st">&quot;your IP seems blocked by telegram&quot;</span></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a><span class="co">#your IP seems blocked by telegram</span></span></code></pre></div>
<p>Bashbot offers the option to recover from broken connections (blocked). Therefore you can provide a function named <code>bashbotBlockRecover()</code> in <code>mycommands.sh</code>, the function is called every time when a broken connection is detected.</p>
<p>Possible actions are: Check if network is working, change IP-Adress or simply wait some time. See <code>mycommnds.sh.dist</code> for an example.</p>
<hr />
<p>@Gnadelwartz</p>
<h2>That's it all guys!</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$$ v1.20-0-g2ab00a2</h4>
<h4>$$VERSION$$ v1.21-dev-34-ga5307e3</h4>
</body>
</html>

142
README.md
View File

@ -67,31 +67,39 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do
### Your really first bashbot in a nutshell
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 command line you should stop reading here :-(
To install and run bashbot you need access to a linux/unix command line with bash, a [Telegram client](https://telegram.org) and a mobile phone [with a Telegram account](https://telegramguide.com/create-a-telegram-account/).
In addition you need a [Telegram client](https://telegram.org) and a mobile phone to [register an account](https://telegramguide.com/create-a-telegram-account/).
If you don't want to register for Telegram you should stop reading here ;-)
First you need to [create a new Telegram Bot token](doc/1_firstbot.md) for your bot and write it down.
After you're registered to Telegram send a message to [@botfather](https://telegram.me/botfather),
[create a new Telegram Bot token](doc/1_firstbot.md) and write it down. You need the token to install the bot.
Now open a linux/unix terminal with bash, create a new directory, change to it and install telegram-bot-bash:
Now open a terminal and check if bash is installed:
```
which bash && echo "bash seems available..."
```
Create a new directory, change to it: ```mkdir tbb; cd tbb``` and download the latest '*.tar.gz' file from
[https://github.com/topkecleon/telegram-bot-bash/releases](https://github.com/topkecleon/telegram-bot-bash/releases). This can be done with the commands:
```bash
wget -q https://github.com/$(wget -q https://github.com/topkecleon/telegram-bot-bash/releases/latest -O - | egrep '/.*/.*/.*tar.gz' -o)
# create bot dir
mkdir mybot
cd mybot
# download latest release with wget or from https://github.com/topkecleon/telegram-bot-bash/releases/latest
wget "https://github.com/$(wget -q "https://github.com/topkecleon/telegram-bot-bash/releases/latest" -O - | egrep '/.*/download/.*/.*tar.gz' -o)"
# Extract the tar archive and go into bot dir
tar -xzf *.tar.gz
cd telegram-bot-bash
# initialize your bot
# Enter your bot token when asked, all other questions can be answered by hitting the \<Return\> key.
./bashbot.sh init
# Now start your bot
./bashbot.sh start
Bottoken is valid ...
Bot Name: yourbotname_bot
Session Name: yourbotname_bot-startbot
Bot started successfully.
```
Extract the '*.tar.gz' file and change to bashbot directory: ```tar -xzf *.tar.gz; cd telegram-bot-bash```,
install bashbot: ```./bashbot.sh init``` and enter your bot token when asked. All other questions can be answered
by hitting the \<Return\> key.
Now open the Telegram App on your mobile phone and start a chatting with your bot (_your bot's username is shown after 'Bot Name:'_):
That's all, now you can start your bot with ```./bashbot.sh start``` and send him messages:
```
/start
@ -110,8 +118,8 @@ For more Information on how to install, customize and use your new bot, read the
### Log files
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.
Bashbot actions are logged to `BASHBOT.log`, Telegram send/receive errors are logged to `ERROR.log`.
Start bashbot in debug mode to get all messages send to / received from Telegram and error messages of bash commands also.
To enable debug mode start bashbot with debug as third argument: `bashbot start debug`
@ -129,18 +137,21 @@ To enable debug mode start bashbot with debug as third argument: `bashbot start
## Security Considerations
Running a Telegram Bot means it is connected to the public and you never know what's send to your Bot.
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 [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells)
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 [Implications of wrong quoting](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells)
Whenever you are processing input 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.
Whenever you are processing input 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 remove unused scripts and examples
from your Bot, e.g. everything in `example/` and disable/remove all not needed bot commands.
**Note:** Up to version v0.941 (mai/22/2020) telegram-bot-bash had a remote code execution (RCE) bug, please update if you use an older version!
see [Issue #125](https://github.com/topkecleon/telegram-bot-bash/issues/125)
It's important to escape or remove `$` in input from user, files or network (_as bashbot does_)
One of the powerful features of unix shells are variable and command substitution using `${}` and`$()`,
this can lead to remote code execution (RCE) or remote information disclosure (RID) bugs if unescaped `$` is included in untrusted input, e.g. `$$` or `$(rm -rf /*)`
One of the most powerful features of unix shells is variable and command substitution using ```${}``` and ```$()```,
but as they are expanded in double quotes, this can lead to RCE and information disclosing bugs in complex scripts like bashbot.
So it's more secure to escape or remove '$' in input from user, files or network.
A powerful tool to improve your scripts is ```shellcheck```. You can [use it online](https://www.shellcheck.net/) or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used extensively in bashbot development to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests.
A powerful tool to improve your scripts is `shellcheck`. You can [use it online](https://www.shellcheck.net/) or
[install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used extensively in bashbot development
to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests.
In addition bashbot has a [test suite](doc/7_develop.md) to check if important functionality is working as expected.
### Use printf whenever possible
@ -148,31 +159,6 @@ In addition bashbot has a [test suite](doc/7_develop.md) to check if important f
If you're writing a script and it is taking external input (from the user as arguments or file system...),
you shouldn't use echo to display it. [Use printf whenever possible](https://unix.stackexchange.com/a/6581)
```bash
# very simple
echo "text with variables. PWD=$PWD"
printf '%s\n' "text with variables. PWD=$PWD"
printf 'text with variables. PWD=%s\n' "$PWD"
-> text with variables. PWD=/home/xxx
# more advanced
FLOAT="1.2346777892864" INTEGER="12345.123"
echo "float=$FLOAT, integer=$INTEGER, PWD=$PWD"
-> float=1.2346777892864, integer=12345.123, PWD=/home/xxx
printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "$FLOAT" "$INTEGER" "$PWD"
-> float=1.23, integer=12345, PWD=/home/xxx
```
### Do not use #!/usr/bin/env bash
**We stay with /bin/bash shebang, because it's more save from security perspective.**
Use of 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.
If you are a BSD / MacOS user or must to use an other bash location, see [Install Bashbot](doc/0_install.md)
### Run your Bot as a restricted user
**I recommend to run your bot as a user, with almost no access rights.**
All files your Bot have write access to are in danger to be overwritten/deleted if your bot is hacked.
@ -183,9 +169,9 @@ For the same reason every file your Bot can read is in danger to be disclosed. R
### Secure your Bot installation
**Your Bot configuration must no be readable from other users.** Everyone who can read your Bots token is able to act as your Bot and has access to all chats the Bot is in!
Everyone with read access to your Bot files can extract your Bots data. Especially your Bot config in ```config.jssh``` must be protected against other users. No one except you should have write access to the Bot files. The Bot should be restricted to have write access to ```count.jssh``` and ```data-bot-bash``` only, all other files must be write protected.
Everyone with read access to your Bot files can extract your Bots data. Especially your Bot config in`config.jssh` must be protected against other users. No one except you should have write access to the Bot files. The Bot should be restricted to have write access to`count.jssh` and `data-bot-bash` only, all other files must be write protected.
To set access rights for your bashbot installation to a reasonable default run ```sudo ./bashbot.sh init``` after every update or change to your installation directory.
To set access rights for your bashbot installation to a reasonable default run`sudo ./bashbot.sh init` after every update or change to your installation directory.
## FAQ
@ -203,29 +189,24 @@ Well, that's a damn good question ... may be because I'm an unix admin from ston
- no database, not event driven, not object oriented ...
### Can I have the single bashbot.sh file back?
At the beginning bashbot was simply the file ```bashbot.sh``` you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more.
At the beginning bashbot was simply the file`bashbot.sh` you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more.
Hey no Problem, if you are finished with your cool bot run ```dev/make-standalone.sh``` to create a stripped down Version of your bot containing only
Hey no Problem, if you are finished with your cool bot run`dev/make-standalone.sh` to create a stripped down Version of your bot containing only
'bashbot.sh' and 'commands.sh'! For more information see [Create a stripped down Version of your Bot](doc/7_develop.md)
### Can I send messages from CLI and scripts?
Of course, you can send messages from CLI and scripts, simply install bashbot as [described here](#Your-really-first-bashbot-in-a-nutshell),
send the message '/start' to set yourself as botadmin and stop the bot with ```./bashbot.sh stop```.
Of course, you can send messages from command line and scripts, simply install bashbot as [described here](#Your-really-first-bashbot-in-a-nutshell),
send the message '/start' to set yourself as botadmin and then stop the bot with `./bashbot.sh stop`.
Run the following commands in your bash shell or script while you are in the installation directory:
Bashbot provides some ready to use scripts for sending messages from command line in `bin/` dir, e.g. `send_message.sh`.
```bash
# prepare bash / script to send commands
export BASHBOT_HOME="$(pwd)"
source ./bashbot.sh source
bin/send_message.sh BOTADMIN "This is my first message send from CLI"
# send me a test message
send_message "$(getConfigKey "botadmin")" "test"
# send me output of a system command
send_message "$(getConfigKey "botadmin")" "$(df -h)"
bin/send_message.sh --help
```
For more information see [Expert Use](doc/8_custom.md)
You can also source bashbot for use in your scripts, for more information see [Expert Use](doc/8_custom.md)
### Blocked by telegram?
@ -244,24 +225,13 @@ nc -w 2 api.telegram.org 443 || echo "your IP seems blocked by telegram"
#your IP seems blocked by telegram
```
Since Version 0.96 bashbot offers the option to recover from broken connections (aka blocked). Therefore you can provide a function
named `bashbotBlockRecover()` in `mycommands.sh`. If the function exists it is called every time when a broken connection is detected.
Bashbot offers the option to recover from broken connections (blocked). Therefore you can provide a function
named `bashbotBlockRecover()` in `mycommands.sh`, the function is called every time when a broken connection is detected.
Possible actions are: Check if network is working, change IP-Adress or simply wait some time.
See `mycommnds.sh.dist` for an example.
If everything seems OK return 0 for retry or any non 0 value to give up.
```bash
# called when bashbot sedn command failed because we can not connect to telegram
# return 0 to retry, return non 0 to give up
bashbotBlockRecover() {
# place your commands to unblock here, e.g. change IP-Adess or simply wait
sleep 60 && return 0 # may be temporary
return 1
}
```
---
@Gnadelwartz
@ -269,4 +239,4 @@ bashbotBlockRecover() {
If you feel that there's something missing or if you found a bug, feel free to submit a pull request!
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-34-ga5307e3

View File

@ -81,44 +81,48 @@ available on www.github.com
### Your really first bashbot in a nutshell
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 command line you should stop
reading here :-(
To install and run bashbot you need access to a linux/unix command line with
bash, a [Telegram client](https://telegram.org) and a mobile phone [with a
Telegram account](https://telegramguide.com/create-a-telegram-account/).
In addition you need a [Telegram client](https://telegram.org) and a mobile
phone to [register an
account](https://telegramguide.com/create-a-telegram-account/).
If you don't want to register for Telegram you should stop reading here ;-)
First you need to [create a new Telegram Bot token](doc/1_firstbot.md) for your
bot and write it down.
After you're registered to Telegram send a message to
[@botfather](https://telegram.me/botfather),
[create a new Telegram Bot token](doc/1_firstbot.md) and write it down. You
need the token to install the bot.
Now open a linux/unix terminal with bash, create a new directory, change to it
and install telegram-bot-bash:
Now open a terminal and check if bash is installed:
```
which bash && echo "bash seems available..."
```
Create a new directory, change to it: ```mkdir tbb; cd tbb``` and download the
latest '*.tar.gz' file from
[https://github.com/topkecleon/telegram-bot-bash/releases](https://github.com/to
pkecleon/telegram-bot-bash/releases). This can be done with the commands:
```bash
wget -q https://github.com/$(wget -q
https://github.com/topkecleon/telegram-bot-bash/releases/latest -O - | egrep
'/.*/.*/.*tar.gz' -o)
# create bot dir
mkdir mybot
cd mybot
# download latest release with wget or from
https://github.com/topkecleon/telegram-bot-bash/releases/latest
wget "https://github.com/$(wget -q
"https://github.com/topkecleon/telegram-bot-bash/releases/latest" -O - | egrep
'/.*/download/.*/.*tar.gz' -o)"
# Extract the tar archive and go into bot dir
tar -xzf *.tar.gz
cd telegram-bot-bash
# initialize your bot
# Enter your bot token when asked, all other questions can be answered by
hitting the \<Return\> key.
./bashbot.sh init
# Now start your bot
./bashbot.sh start
Bottoken is valid ...
Bot Name: yourbotname_bot
Session Name: yourbotname_bot-startbot
Bot started successfully.
```
Extract the '*.tar.gz' file and change to bashbot directory: ```tar -xzf
*.tar.gz; cd telegram-bot-bash```,
install bashbot: ```./bashbot.sh init``` and enter your bot token when asked.
All other questions can be answered
by hitting the \<Return\> key.
Now open the Telegram App on your mobile phone and start a chatting with your
bot (_your bot's username is shown after 'Bot Name:'_):
That's all, now you can start your bot with ```./bashbot.sh start``` and send
him messages:
```
/start
@ -139,9 +143,10 @@ the [Documentation](#Documentation)
### Log files
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.
Bashbot actions are logged to `BASHBOT.log`, Telegram send/receive errors are
logged to `ERROR.log`.
Start bashbot in debug mode to get all messages send to / received from
Telegram and error messages of bash commands also.
To enable debug mode start bashbot with debug as third argument: `bashbot start
debug`
@ -163,33 +168,34 @@ Running a Telegram Bot means it is connected to the public and you never know
what's send to your Bot.
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 [Implications of wrong
Bot as a proof of concept.
Bash programmers often struggle with 'quoting hell' and globbing,
see [Implications of wrong
quoting](https://unix.stackexchange.com/questions/171346/security-implications-o
f-forgetting-to-quote-a-variable-in-bash-posix-shells)
Whenever you are processing input 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.
network) you must be as careful as possible,
e.g. set IFS appropriate, disable globbing (set -f) and quote everything. In
addition remove unused scripts and examples
from your Bot, e.g. everything in `example/` and disable/remove all not needed
bot commands.
**Note:** Up to version v0.941 (mai/22/2020) telegram-bot-bash had a remote
code execution (RCE) bug, please update if you use an older version!
see [Issue #125](https://github.com/topkecleon/telegram-bot-bash/issues/125)
It's important to escape or remove `$` in input from user, files or network
(_as bashbot does_)
One of the powerful features of unix shells are variable and command
substitution using `${}` and`$()`,
this can lead to remote code execution (RCE) or remote information disclosure
(RID) bugs if unescaped `$` is included in untrusted input, e.g. `$$` or `$(rm
-rf /*)`
One of the most powerful features of unix shells is variable and command
substitution using ```${}``` and ```$()```,
but as they are expanded in double quotes, this can lead to RCE and information
disclosing bugs in complex scripts like bashbot.
So it's more secure to escape or remove '$' in input from user, files or
network.
A powerful tool to improve your scripts is ```shellcheck```. You can [use it
online](https://www.shellcheck.net/) or [install shellcheck
A powerful tool to improve your scripts is `shellcheck`. You can [use it
online](https://www.shellcheck.net/) or
[install shellcheck
locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used
extensively in bashbot development to ensure a high code quality, e.g. it's not
allowed to push changes without passing all shellcheck tests.
extensively in bashbot development
to ensure a high code quality, e.g. it's not allowed to push changes without
passing all shellcheck tests.
In addition bashbot has a [test suite](doc/7_develop.md) to check if important
functionality is working as expected.
@ -200,35 +206,6 @@ arguments or file system...),
you shouldn't use echo to display it. [Use printf whenever
possible](https://unix.stackexchange.com/a/6581)
```bash
# very simple
echo "text with variables. PWD=$PWD"
printf '%s\n' "text with variables. PWD=$PWD"
printf 'text with variables. PWD=%s\n' "$PWD"
-> text with variables. PWD=/home/xxx
# more advanced
FLOAT="1.2346777892864" INTEGER="12345.123"
echo "float=$FLOAT, integer=$INTEGER, PWD=$PWD"
-> float=1.2346777892864, integer=12345.123, PWD=/home/xxx
printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "$FLOAT"
"$INTEGER" "$PWD"
-> float=1.23, integer=12345, PWD=/home/xxx
```
### Do not use #!/usr/bin/env bash
**We stay with /bin/bash shebang, because it's more save from security
perspective.**
Use of 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.
If you are a BSD / MacOS user or must to use an other bash location, see
[Install Bashbot](doc/0_install.md)
### Run your Bot as a restricted user
**I recommend to run your bot as a user, with almost no access rights.**
All files your Bot have write access to are in danger to be overwritten/deleted
@ -246,13 +223,13 @@ can read your Bots token is able to act as your Bot and has access to all chats
the Bot is in!
Everyone with read access to your Bot files can extract your Bots data.
Especially your Bot config in ```config.jssh``` must be protected against other
Especially your Bot config in`config.jssh` must be protected against other
users. No one except you should have write access to the Bot files. The Bot
should be restricted to have write access to ```count.jssh``` and
```data-bot-bash``` only, all other files must be write protected.
should be restricted to have write access to`count.jssh` and `data-bot-bash`
only, all other files must be write protected.
To set access rights for your bashbot installation to a reasonable default run
```sudo ./bashbot.sh init``` after every update or change to your installation
To set access rights for your bashbot installation to a reasonable default
run`sudo ./bashbot.sh init` after every update or change to your installation
directory.
## FAQ
@ -277,37 +254,33 @@ health status
- no database, not event driven, not object oriented ...
### Can I have the single bashbot.sh file back?
At the beginning bashbot was simply the file ```bashbot.sh``` you can copy
At the beginning bashbot was simply the file`bashbot.sh` you can copy
everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh',
'modules/*.sh' and much more.
Hey no Problem, if you are finished with your cool bot run
```dev/make-standalone.sh``` to create a stripped down Version of your bot
Hey no Problem, if you are finished with your cool bot
run`dev/make-standalone.sh` to create a stripped down Version of your bot
containing only
'bashbot.sh' and 'commands.sh'! For more information see [Create a stripped
down Version of your Bot](doc/7_develop.md)
### Can I send messages from CLI and scripts?
Of course, you can send messages from CLI and scripts, simply install bashbot
as [described here](#Your-really-first-bashbot-in-a-nutshell),
send the message '/start' to set yourself as botadmin and stop the bot with
```./bashbot.sh stop```.
Of course, you can send messages from command line and scripts, simply install
bashbot as [described here](#Your-really-first-bashbot-in-a-nutshell),
send the message '/start' to set yourself as botadmin and then stop the bot
with `./bashbot.sh stop`.
Run the following commands in your bash shell or script while you are in the
installation directory:
Bashbot provides some ready to use scripts for sending messages from command
line in `bin/` dir, e.g. `send_message.sh`.
```bash
# prepare bash / script to send commands
export BASHBOT_HOME="$(pwd)"
source ./bashbot.sh source
bin/send_message.sh BOTADMIN "This is my first message send from CLI"
# send me a test message
send_message "$(getConfigKey "botadmin")" "test"
# send me output of a system command
send_message "$(getConfigKey "botadmin")" "$(df -h)"
bin/send_message.sh --help
```
For more information see [Expert Use](doc/8_custom.md)
You can also source bashbot for use in your scripts, for more information see
[Expert Use](doc/8_custom.md)
### Blocked by telegram?
@ -329,28 +302,16 @@ nc -w 2 api.telegram.org 443 || echo "your IP seems blocked by telegram"
#your IP seems blocked by telegram
```
Since Version 0.96 bashbot offers the option to recover from broken connections
(aka blocked). Therefore you can provide a function
named `bashbotBlockRecover()` in `mycommands.sh`. If the function exists it is
called every time when a broken connection is detected.
Bashbot offers the option to recover from broken connections (blocked).
Therefore you can provide a function
named `bashbotBlockRecover()` in `mycommands.sh`, the function is called every
time when a broken connection is detected.
Possible actions are: Check if network is working, change IP-Adress or simply
wait some time.
See `mycommnds.sh.dist` for an example.
If everything seems OK return 0 for retry or any non 0 value to give up.
```bash
# called when bashbot sedn command failed because we can not connect to telegram
# return 0 to retry, return non 0 to give up
bashbotBlockRecover() {
# place your commands to unblock here, e.g. change IP-Adess or simply
wait
sleep 60 && return 0 # may be temporary
return 1
}
```
---
@Gnadelwartz
@ -359,4 +320,4 @@ wait
If you feel that there's something missing or if you found a bug, feel free to
submit a pull request!
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-34-ga5307e3

View File

@ -4,7 +4,7 @@
# this addon counts how many files, e.g. stickers, are sent to
# a chat and takes actions if threshold is reached
#
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-44-gb04ee84
# used events:
#
@ -44,7 +44,7 @@ fi
# register on startbot
if [[ "$1" = "start"* ]]; then
ANTIFL_ADMIN="$(< "${BOTADMIN}")"
ANTIFL_ADMIN="$(getConfigKey "botadmin")"
#load existing chat settings on start
jssh_readDB "ANTIFL_CHATS" "addons/$ANTIFL_ME"

View File

@ -4,7 +4,7 @@
# Addons can register to bashbot events at startup
# by providing their name and a callback per event
#
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-44-gb04ee84
#
# If an event occurs each registered event function is called.
#
@ -60,6 +60,7 @@ fi
if [[ "$1" = "start"* ]]; then
# register to reply
BASHBOT_EVENT_REPLY["${EXAMPLE_ME}"]="${EXAMPLE_ME}_reply"
EXAMPLE_ADMIN="$(getConfigKey "botadmin")"
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
@ -87,7 +88,7 @@ if [[ "$1" = "start"* ]]; then
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
example_after5min(){
send_markdown_message "$(< "${BOTADMIN}")" "This is a one time event after 5 Minutes!" &
send_markdown_message "${EXAMPLE_ADMIN}" "This is a one time event after 5 Minutes!" &
}
BASHBOT_EVENT_TIMER["${EXAMPLE_ME}every2min","2"]="${EXAMPLE_ME}_every2min"
@ -95,7 +96,7 @@ if [[ "$1" = "start"* ]]; then
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
example_every2min(){
send_markdown_message "$(< "${BOTADMIN}")" "This a a every 2 minute event ..." &
send_markdown_message "${EXAMPLE_ADMIN}" "This a a every 2 minute event ..." &
}
# register to send
@ -109,6 +110,6 @@ if [[ "$1" = "start"* ]]; then
# Note: do not call any send message functions from EVENT_SEND!
example_log(){
local send="$1"; shift
echo "$(date): Type: ${send} Args: $*" >>"${EXAMPLE_LOG}"
printf "%s: Type: %s Args: %s\n" "$(date)" "${send}" "$*" >>"${EXAMPLE_LOG}"
}
fi

View File

@ -1,56 +1,62 @@
#!/bin/bash
# file: bashbot.sh
# do not edit, this file will be overwritten on update
# bashbot, the Telegram bot written in bash.
# Written by Drew (@topkecleon) KayM (@gnadelwartz).
# Also contributed: Daniil Gentili (@danogentili), JuanPotato, BigNerd95, TiagoDanin, iicc1.
# https://github.com/topkecleon/telegram-bot-bash
# Depends on JSON.sh (http://github.com/dominictarr/JSON.sh) (MIT/Apache),
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
##################################################################
#
#### $$VERSION$$ v1.20-0-g2ab00a2
# File: bashbot.sh
# Note: DO NOT EDIT! this file will be overwritten on update
#
# Description:
# bashbot, the Telegram bot written in bash.
# Written by Drew (@topkecleon) KayM (@gnadelwartz).
# Also contributed: Daniil Gentili (@danogentili), JuanPotato,
# BigNerd95, TiagoDanin, iicc1.
# https://github.com/topkecleon/telegram-bot-bash
#
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
# Exit Codes:
# - 0 success (hopefully)
# - 1 can't change to dir
# - 2 can't write to tmp, count or token
# - 3 user / command / file not found
# - 4 unknown command
# - 5 cannot connect to telegram bot
# - 6 mandatory module not found
# - 7 can't get bottoken
# - 8 curl/wget missing
# - 10 not bash!
# shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117
# 0 - success (hopefully)
# 1 - can't change to dir
# 2 - can't write to tmp, count or token
# 3 - user / command / file not found
# 4 - unknown command
# 5 - cannot connect to telegram bot
# 6 - mandatory module not found
# 7 - can't get bottoken
# 8 - curl/wget missing
# 10 - not bash!
#
#### $$VERSION$$ v1.21-dev-43-g79f58cd
##################################################################
# shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059
# emmbeded system may claim bash but it is not
# check for bash like ARRAY handlung
if ! (unset a; set -A a a; eval "a=(a b)"; eval '[ -n "${a[1]}" ]'; ) > /dev/null 2>&1; then
echo "Error: Current shell does not support ARRAY's, may be busbox ash shell. pls install a real bash!";
printf "Error: Current shell does not support ARRAY's, may be busbox ash shell. pls install a real bash!\n"
exit 10
fi
# are we running in a terminal?
NN="\n"
if [ -t 1 ] && [ -n "$TERM" ]; then
CLEAR='clear'
INTERACTIVE='yes'
RED='\e[31m'
GREEN='\e[32m'
ORANGE='\e[35m'
GREY='\e[1;30m'
NC='\e[0m'
NN="${NC}\n"
fi
# telegram uses utf-8 characters, check if we have an utf-8 charset
if [ "${LANG}" = "${LANG%[Uu][Tt][Ff]*}" ]; then
echo -e "${ORANGE}Warning: Telegram uses utf-8, but looks like you are using non utf-8 locale:${NC} ${LANG}"
printf "${ORANGE}Warning: Telegram uses utf-8, but looks like you are using non utf-8 locale:${NC} ${LANG}\n"
fi
# we need some bash 4+ features, check for old bash by feature
if [ "$({ LC_ALL=C.utf-8 echo -e "\u1111"; } 2>/dev/null)" = "\u1111" ]; then
echo -e "${ORANGE}Warning: Missing unicode '\uxxxx' support, missing C.utf-8 locale or to old bash version.${NC}"
printf "${ORANGE}Warning: Missing unicode '\uxxxx' support, missing C.utf-8 locale or to old bash version.${NN}"
fi
@ -89,15 +95,17 @@ check_token(){
[[ "${1}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]] && return 0
return 1
}
# log $1 to ERRORLOG with date
log_error(){ printf "%s: %b\n" "$(date)" "$*" >>"${ERRORLOG}"; }
log_debug(){ printf "%s: %b\n" "$(date)" "$*" >>"${DEBUGLOG}"; }
log_message(){ printf "\n%s: %b\n" "$(date)" "$*" >>"${MESSAGELOG}"; }
log_update(){ printf "%s: %b\n" "$(date)" "$*" >>"${UPDATELOG}"; }
# log $1 with date
log_error(){ printf "%s: %s\n" "$(date)" "$*" >>"${ERRORLOG}"; }
log_debug(){ printf "%s: %s\n" "$(date)" "$*" >>"${DEBUGLOG}"; }
log_update(){ printf "%s: %s\n" "$(date)" "$*" >>"${UPDATELOG}"; }
# log $1 with date, special first \n
log_message(){ printf "\n%s: %s\n" "$(date)" "$*" >>"${MESSAGELOG/\\n/$'\n'}"; }
# additional tests if we run in debug mode
export BASHBOTDEBUG
# debug should always last argument
[[ "${BASH_ARGV[0]}" == *"debug"* ]] && BASHBOTDEBUG="yes"
# $1 where $2 command $3 may debug
# shellcheck disable=SC2094
debug_checks(){ {
@ -117,7 +125,7 @@ debug_checks(){ {
# some linux, e.g. manajro seems not to have C locale activated by default
if _exists locale && [ "$(locale -a | grep -c -e "^C$" -e "^C.utf8$")" -lt 2 ]; then
echo -e "${ORANGE}Warning: locale ${NC}${GREY}C${NC}${ORANGE} and/or ${NC}${GREY}C.utf8${NC}${ORANGE} seems missing, use \"${NC}${GREY}locale -a${NC}${ORANGE}\" to show what locales are installed on your system.${NC}"
printf "${ORANGE}Warning: locale ${NC}${GREY}C${NC}${ORANGE} and/or ${NC}${GREY}C.utf8${NC}${ORANGE} seems missing, use \"${NC}${GREY}locale -a${NC}${ORANGE}\" to show what locales are installed on your system.${NN}"
fi
# get location and name of bashbot.sh
@ -134,10 +142,10 @@ if [ "${SCRIPT}" != "${REALME}" ] || [ "$1" = "source" ]; then
fi
BOTCOMMANDS="start, stop, status, help, init, suspendback, resumeback, killback"
[[ -z "$1" && -z "${SOURCE}" ]] && echo -e "${ORANGE}Available commands: ${GREY}${BOTCOMMANDS}${NC}" && exit
[[ -z "$1" && -z "${SOURCE}" ]] && printf "${ORANGE}Available commands: ${GREY}${BOTCOMMANDS}${NN}" && exit
if [ "$1" = "help" ]; then
HELP="${BASHBOT_HOME:-.}/README"
if [ -n "${CLEAR}" ];then
if [ -n "${INTERACTIVE}" ];then
_exists w3m && w3m "$HELP.html" && exit
_exists lynx && lynx "$HELP.html" && exit
_exists less && less "$HELP.txt" && exit
@ -157,23 +165,31 @@ fi
ADDONDIR="${BASHBOT_ETC:-.}/addons"
RUNUSER="${USER}" # USER is overwritten by bashbot array :-(, save original
# OK everything setup, lets start
# OK, ENVIRONMENT is set up, let's do some additional tests
if [[ -z "${SOURCE}" && -z "$BASHBOT_HOME" ]] && ! cd "${RUNDIR}" ; then
echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}"
printf "${RED}ERROR: Can't change to ${RUNDIR} ...${NN}"
exit 1
else
RUNDIR="."
fi
if [ ! -w "." ]; then
echo -e "${ORANGE}WARNING: ${RUNDIR} is not writeable!${NC}"
printf "${ORANGE}WARNING: ${RUNDIR} is not writeable!${NN}"
ls -ld .
fi
# Setup and check environment if BOTTOKEN is NOT set
# check if JSON.sh is available
JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}"
[[ "${JSONSHFILE}" != *"/JSON.sh" ]] &&\
printf "${RED}ERROR:${NC} ${JSONSHFILE} ${RED}does not end with${NC} JSONS.sh\n" &&\
exit 3
[ ! -x "${JSONSHFILE}" ] &&\
printf "${RED}ERROR:${NC} ${JSONSHFILE} ${RED}seems not to exist, are we in dev environment?${NN}${GREY}%s${NN}"\
"JSONSHFILE environment variable points to wrong file or bashbot is not installed correct, see doc/0_install.md\n" &&\
exit 3
# file locations based on ENVIRONMENT
BOTCONFIG="${BASHBOT_ETC:-.}/botconfig"
TOKENFILE="${BASHBOT_ETC:-.}/token"
BOTADMIN="${BASHBOT_ETC:-.}/botadmin"
BOTACL="${BASHBOT_ETC:-.}/botacl"
DATADIR="${BASHBOT_VAR:-.}/data-bot-bash"
BLOCKEDFILE="${BASHBOT_VAR:-.}/blocked"
@ -181,37 +197,31 @@ COUNTFILE="${BASHBOT_VAR:-.}/count"
LOGDIR="${RUNDIR:-.}/logs"
# CREATE botconfig if not exist
# assume everything already set up correctly if TOKEN is set
if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then
# BOTCONFIG does not exist, create
printf '["bot_config_key"]\t"config_key_value"\n' >>"${BOTCONFIG}.jssh"
# convert old token
if [ -r "${TOKENFILE}" ]; then
token="$(< "${TOKENFILE}")"
# no old token, ask user
elif [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then
echo "Running headless, set BOTTOKEN or run ${SCRIPT} init first!"
# ask user for bot token
if [ -z "${INTERACTIVE}" ] && [ "$1" != "init" ]; then
printf "Running headless, set BOTTOKEN or run ${SCRIPT} init first!\n"
exit 2
else
${CLEAR}
echo -e "${RED}TOKEN MISSING.${NC}"
echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}"
printf "${RED}ENTER BOT TOKEN...${NN}"
printf "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NN}"
read -r token
printf "\n"
fi
[ -n "${token}" ] && printf '["bottoken"]\t"%s"\n' "${token}" >> "${BOTCONFIG}.jssh"
# no botadmin, setup botadmin
if [ -z "$(getConfigKey "botadmin")" ]; then
# convert old admin
if [ -r "${BOTADMIN}" ]; then
admin="$(< "${BOTADMIN}")"
elif [ -z "${CLEAR}" ]; then
echo "Running headless, set botadmin to AUTO MODE!"
# ask user for bot admin
if [ -z "${INTERACTIVE}" ]; then
printf "Running headless, set botadmin to AUTO MODE!\n"
else
${CLEAR}
echo -e "${RED}BOTADMIN MISSING.${NC}"
echo -e "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NC}"
echo -e "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NC}"
printf "${RED}ENTER BOT ADMIN...${NN}"
printf "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR PRESS ENTER\nTO MAKE FIRST USER TYPING '/start' BOT ADMIN${NN}?\b"
read -r admin
fi
[ -z "${admin}" ] && admin='?'
@ -219,26 +229,22 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then
fi
# setup botacl file
if [ ! -f "${BOTACL}" ]; then
echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}"
printf "${GREY}Create initial ${BOTACL} file.${NN}"
printf '\n' >"${BOTACL}"
fi
# setup data dir file
if [ ! -d "${DATADIR}" ]; then
mkdir "${DATADIR}"
elif [ ! -w "${DATADIR}" ]; then
echo -e "${RED}ERROR: Can't write to ${DATADIR}!.${NC}"
printf "${RED}ERROR: Can't write to ${DATADIR}!.${NN}"
ls -ld "${DATADIR}"
exit 2
fi
# setup count file
if [ ! -f "${COUNTFILE}.jssh" ]; then
printf '["counted_user_chat_id"]\t"num_messages_seen"\n' >> "${COUNTFILE}.jssh"
# convert old file on creation
if [ -r "${COUNTFILE}" ];then
sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh"
fi
elif [ ! -w "${COUNTFILE}.jssh" ]; then
echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}"
printf "${RED}ERROR: Can't write to ${COUNTFILE}!.${NN}"
ls -l "${COUNTFILE}.jssh"
exit 2
fi
@ -262,13 +268,14 @@ if [ -z "${BOTTOKEN}" ]; then
BOTTOKEN="$(getConfigKey "bottoken")"
if [ -z "${BOTTOKEN}" ]; then
BOTERROR="Warning: can't get bot token, try to recover working config..."
echo -e "${ORANGE}${BOTERROR}${NC} \c"
printf "${ORANGE}${BOTERROR}${NC} "
if [ -r "${BOTCONFIG}.jssh.ok" ]; then
log_error "${BOTERROR}"
cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh"; echo "OK"
mv "${BOTCONFIG}.jssh" "${BOTCONFIG}.jssh.bad"
cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh"; printf "OK\n"
BOTTOKEN="$(getConfigKey "bottoken")"
else
echo -e "\n${RED}Error: Missing bot token! remove ${BOTCONFIG}.jssh and run \"bashbot.sh init\" may fix it.${NC}"
printf "\n${RED}Error: Can't recover from missing bot token! Remove ${BOTCONFIG}.jssh and run${NC} bashbot.sh init\n"
exit 7
fi
fi
@ -276,20 +283,20 @@ fi
# BOTTOKEN format checks
if ! check_token "${BOTTOKEN}"; then
echo -e "${ORANGE}Warning: your bottoken may incorrect. it should have the following format:${NC}"
echo -e "${GREY}123456789${RED}:${GREY}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4${ORANGE} => ${NC}\c"
echo -e "${GREY}8-10 digits${RED}:${GREY}35 alphanumeric characters + '_-'${NC}"
echo -e "${ORANGE}Your current token is: '${GREY}^$(cat -ve <<<"${BOTTOKEN//:/${RED}:${GREY}}")${ORANGE}'${NC}"
printf "\n${ORANGE}Warning: Your bot token is incorrect, it should have the following format:${NC}\n"
printf "<your_bot_id>${RED}:${NC}<35_alphanumeric_characters-hash> ${RED}e.g. =>${NC} 123456789${RED}:${NC}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4\n\n"
printf "${GREY}Your bot token: '${NC}${BOTTOKEN//:/${RED}:${NC}}'\n"
if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}: ]]; then
echo -e "${ORANGE}Possible problem in the digits part, len is $(($(wc -c <<<"${BOTTOKEN%:*}")-1))${NC}"
[ -n "$(getConfigKey "botid")" ] && echo -e "${GREY}Did you mean: \"${NC}$(getConfigKey "botid")${GREY}\" ?${NC}"
printf "${GREY}\tHint: Bot id len: ${NC}$(($(wc -c <<<"${BOTTOKEN%:*}")-1)) ${GREY}but should be${NC} 8-10\n"
[ -n "$(getConfigKey "botid")" ] && printf "\t${GREEN}Did you mean: \"${NC}$(getConfigKey "botid")${GREEN}\" ?${NN}"
fi
[[ ! "${BOTTOKEN}" =~ :[a-zA-Z0-9_-]{35}$ ]] &&\
echo -e "${ORANGE}Possible problem in the characters part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NC}"
printf "${GREY}\tHint: Hash contains invalid character or is not${NC} 35 ${GREY}characters long, hash len is ${NC}$(($(wc -c <<<"${BOTTOKEN#*:}")-1))\n"
printf "\n"
fi
##################
# here we start with the real stuff
BASHBOT_RETRY="" # retry by default
@ -303,7 +310,7 @@ GETFILE_URL=$URL'/getFile'
#################
# BASHBOT COMMON functions
declare -rx SCRIPT SCRIPTDIR MODULEDIR RUNDIR ADDONDIR TOKENFILE BOTADMIN BOTACL DATADIR COUNTFILE
declare -rx SCRIPT SCRIPTDIR MODULEDIR RUNDIR ADDONDIR BOTACL DATADIR COUNTFILE
declare -rx BOTTOKEN URL ME_URL UPD_URL GETFILE_URL
declare -ax CMD
@ -317,7 +324,7 @@ export res CAPTION ME
COMMANDS="${BASHBOT_ETC:-.}/commands.sh"
if [ -z "${SOURCE}" ]; then
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}"
printf "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NN}"
ls -l "${COMMANDS}"
exit 3
fi
@ -338,11 +345,11 @@ done
# do we have BSD sed
if ! sed '1ia' </dev/null 2>/dev/null; then
echo -e "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NC}"
printf "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NN}"
fi
#jsonDB is now mandatory
if ! _is_function jssh_newDB ; then
echo -e "${RED}ERROR: Mandatory module jsonDB is missing or not readable!"
printf "${RED}ERROR: Mandatory module jsonDB is missing or not readable!${NN}"
exit 6
fi
@ -523,9 +530,9 @@ else
else
# ups, no curl AND no wget
if [ -n "${BASHBOT_WGET}" ]; then
echo -e "${RED}Error: You set BASHBOT_WGET but no wget found!${NC}"
printf "${RED}Error: You set BASHBOT_WGET but no wget found!${NN}"
else
echo -e "${RED}Error: curl and wget not found, install curl!${NC}"
printf "${RED}Error: curl and wget not found, install curl!${NN}"
fi
exit 8
fi
@ -649,7 +656,7 @@ getBotName() {
# save botname and id
setConfigKey "botname" "${BOTARRAY["result","username"]}"
setConfigKey "botid" "${BOTARRAY["result","id"]}"
echo "${BOTARRAY["result","username"]}"
printf "${BOTARRAY["result","username"]}\n"
}
# pure bash implementation, done by KayM (@gnadelwartz)
@ -1016,7 +1023,7 @@ process_message() {
declare -A BASHBOTBLOCKED
export BASHBOT_UPDATELOG="${BASHBOT_UPDATELOG-nolog}" # allow to be ""
start_bot() {
local DEBUGMSG ADMIN OFFSET=0
local DEBUGMSG OFFSET=0
# adaptive sleep defaults
local nextsleep="100"
local stepsleep="${BASHBOT_SLEEP_STEP:-100}"
@ -1055,8 +1062,7 @@ start_bot() {
# read blocked users
jssh_readDB_async "BASHBOTBLOCKED" "${BLOCKEDFILE}"
# inform botadmin about start
ADMIN="$(getConfigKey "botadmin")"
[ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") started ..." &
send_normal_message "$(getConfigKey "botadmin")" "Bot $(getConfigKey "botname") started ..." &
##########
# bot is ready, start processing updates ...
while true; do
@ -1102,95 +1108,67 @@ bot_init() {
[ -n "${BASHBOT_HOME}" ] && cd "${BASHBOT_HOME}" || exit 1
local DEBUG="$1"
# upgrade from old version
echo "Check for Update actions ..."
local OLDTMP="${BASHBOT_VAR:-.}/tmp-bot-bash"
[ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${DATADIR}"; rmdir "${OLDTMP}"; }
# no more existing modules
[ -f "modules/inline.sh" ] && rm -f "modules/inline.sh"
# currently no action
printf "Check for Update actions ...\n"
printf "Done.\n"
# load addons on startup
echo "Done."
echo "Initialize modules and addons ..."
printf "Initialize modules and addons ...\n"
for addons in "${ADDONDIR:-.}"/*.sh ; do
# shellcheck source=./modules/aliases.sh
[ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}"
done
echo "Done."
if [[ ! -d "logs" ]]; then
echo "Move Logfiles ..."
mkdir logs 2>/dev/null
for MVLOG in DEBUG.log MESSAGE.log ERROR.log BASHBOT.log
do
[ -f "${MVLOG}" ] && mv "${MVLOG}" logs 2>/dev/null
done
echo "Done."
fi
printf "Done.\n"
# setup bashbot
[[ "${UID}" -eq "0" ]] && RUNUSER="nobody"
echo -n "Enter User to run bashbot [$RUNUSER]: "
printf "Enter User to run bashbot [$RUNUSER]: "
read -r TOUSER
[ -z "$TOUSER" ] && TOUSER="$RUNUSER"
if ! id "$TOUSER" &>/dev/null; then
echo -e "${RED}User \"$TOUSER\" not found!${NC}"
printf "${RED}User \"$TOUSER\" not found!${NN}"
exit 3
else
# shellcheck disable=SC2009
oldbot="$(ps -fu "$TOUSER" | grep startbot | grep -v -e 'grep' -e '\-startbot' )"
[ -n "${oldbot}" ] && \
echo -e "${ORANGE}Warning: At least one not upgraded TMUX bot is running! You must stop it with kill command:${NC}\\n${oldbot}"
echo "Adjusting files and permissions for user \"${TOUSER}\" ..."
printf "Adjusting files and permissions for user \"${TOUSER}\" ...\n"
[ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc"
chown -R "$TOUSER" . ./*
chmod 711 .
chmod -R o-w ./*
chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTADMIN}" logs "${LOGDIR}/"*.log 2>/dev/null
chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null
chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" logs "${LOGDIR}/"*.log 2>/dev/null
chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTACL}" 2>/dev/null
# jsshDB must writeable by owner
find . -name '*.jssh*' -exec chmod u+w \{\} +
echo "Done."
chown -R "$TOUSER" . ./*
printf "Done.\n"
fi
# ask to check bottoken online
if [ -z "$(getConfigKey "botid")" ]; then
echo -e "Seems to be your first init. Should I verify your bot token online? (y/N) N\b\c"
printf "Seems to be your first init. Should I verify your bot token online? (y/N) N\b"
read -r ANSWER
if [[ "${ANSWER}" =~ ^[Yy] ]]; then
echo -e "${GREEN}Contacting telegram to verify your bot token ...${NC}"
printf "${GREEN}Contacting telegram to verify your bot token ...${NN}"
$0 botname
fi
fi
# check if botconf if seems valid
echo -e "${GREEN}This is your bot config:${NC}"
# check if botconf seems valid
printf "${GREEN}This is your bot config:${NN}"
sed 's/^/\t/' "${BOTCONFIG}.jssh" | grep -vF '["bot_config_key"]'
if [[ "$(getConfigKey "bottoken")" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ && "$(getConfigKey "botadmin")" =~ ^[0-9]+$ ]]; then
echo -e "Bot config seems to be valid. Should I make a backup copy? (Y/n) Y\b\c"
if check_token "$(getConfigKey "bottoken")" && [[ "$(getConfigKey "botadmin")" =~ ^[0-9]+$ ]]; then
printf "Bot config seems to be valid. Should I make a backup copy? (Y/n) Y\b"
read -r ANSWER
if [[ -z "${ANSWER}" || "${ANSWER}" =~ ^[^Nn] ]]; then
echo "Copy bot config to ${BOTCONFIG}.jssh.ok ..."
printf "Copy bot config to ${BOTCONFIG}.jssh.ok ...\n"
cp "${BOTCONFIG}.jssh" "${BOTCONFIG}.jssh.ok"
fi
else
echo -e "${ORANGE}Bot config may not complete, pls check.${NC}"
printf "${ORANGE}Bot config may incomplete, pls check.${NN}"
fi
# show result
ls -ld "${DATADIR}" "${LOGDIR}" ./*.jssh* ./*.sh 2>/dev/null
}
if ! _is_function send_message ; then
echo -e "${RED}ERROR: send_message is not available, did you deactivate ${MODULEDIR}/sendMessage.sh?${NC}"
printf "${RED}ERROR: send_message is not available, did you deactivate ${MODULEDIR}/sendMessage.sh?${NN}"
exit 1
fi
# get location of JSON.sh, download if not exist
JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}"
[[ "${JSONSHFILE}" != *"/JSON.sh" ]] && echo -e "${RED}ERROR: \"${JSONSHFILE}\" ends not with \"JSONS.sh\".${NC}" && exit 3
if [ ! -f "${JSONSHFILE}" ]; then
echo "Seems to be first run, Downloading ${JSONSHFILE}..."
[ "${SCRIPTDIR}/JSON.sh/JSON.sh" = "${JSONSHFILE}" ] &&\
mkdir "${SCRIPTDIR}/JSON.sh" 2>/dev/null && chmod +w "${SCRIPTDIR}/JSON.sh"
getJson "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" >"${JSONSHFILE}"
chmod +x "${JSONSHFILE}"
fi
# check if JSON.awk exist and has x flag
JSONAWKFILE="${JSONSHFILE%.sh}.awk"
if [ -x "${JSONAWKFILE}" ] && _exists awk ; then
@ -1210,24 +1188,24 @@ if [ -z "${SOURCE}" ]; then
ME="$(getBotName)"
if [ -n "${ME}" ]; then
# ok we have a connection and got botname, save it
[ -n "${CLEAR}" ] && echo -e "${GREY}Bottoken is valid ...${NC}"
[ -n "${INTERACTIVE}" ] && printf "${GREY}Bottoken is valid ...${NN}"
jssh_updateKeyDB "botname" "${ME}" "${BOTCONFIG}"
rm -f "${BOTCONFIG}.jssh.flock"
else
echo -e "${GREY}Info: Can't get Botname from Telegram, try cached one ...${NC}"
printf "${GREY}Info: Can't get Botname from Telegram, try cached one ...${NN}"
ME="$(getConfigKey "botname")"
if [ -z "$ME" ]; then
echo -e "${RED}ERROR: No cached botname, can't continue! ...${NC}"
printf "${RED}ERROR: No cached botname, can't continue! ...${NN}"
exit 1
fi
fi
[ -n "${CLEAR}" ] && printf "Bot Name: %s\n" "${ME}"
[ -n "${INTERACTIVE}" ] && printf "Bot Name: %s\n" "${ME}"
[ "$1" = "botname" ] && exit
;;&
# used to send output of background and interactive to chats
"outproc") # $2 chat_id $3 identifier of job, internal use only!
[ -z "$3" ] && echo "No job identifier" && exit 3
[ -z "$2" ] && echo "No chat to send to" && exit 3
[ -z "$3" ] && printf "No job identifier\n" && exit 3
[ -z "$2" ] && printf "No chat to send to\n" && exit 3
ME="$(getConfigKey "botname")"
# read until terminated
while read -r line ;do
@ -1251,45 +1229,15 @@ if [ -z "${SOURCE}" ]; then
debug_checks "end init" "$@"
exit
;;
# print usage stats
"stats")
echo -e "${ORANGE}stats deprecated, see bin/bashbot_stats --help${NC}"
ME="$(getConfigKey "botname")"
declare -A STATS
jssh_readDB_async "STATS" "${COUNTFILE}"
for MSG in ${!STATS[*]}
do
[[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue
(( USERS++ ))
done
for MSG in ${STATS[*]}
do
(( MESSAGES+=MSG ))
done
if [ "${USERS}" != "" ]; then
echo "A total of ${MESSAGES} messages from ${USERS} users are processed."
else
echo "No one used your bot so far ..."
fi
jssh_readDB_async "STATS" "${BLOCKEDFILE}"
for MSG in ${!STATS[*]}
do
[[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue
(( BLOCKS++ ))
done
if [ "${BLOCKS}" != "" ]; then
echo -e "Note: ${BLOCKS} users are blocked by your bot:${GREY}"
sort -r "${BLOCKEDFILE}.jssh"
echo -e "${NC}\c"
fi
# show user created bot stats
_exec_if_function my_bashbot_stats "$@"
debug_checks "end $1" "$@"
# stats deprecated
"stats"|"count")
printf "${ORANGE}Stats is a separate command now, see bin/bashbot_stats.sh --help${NN}"
"${BASHBOT_HOME:-.}"/bin/bashbot_stats.sh --help
exit
;;
# send message to all users
# broadcast deprecated
'broadcast')
echo -e "${ORANGE}Broadcast is a separate command now, see ${BASHBOT_HOME:-.}/bin/send_broadcast.sh --help${NC}"
printf "${ORANGE}Broadcast is a separate command now, see bin/send_broadcast.sh --help${NN}"
"${BASHBOT_HOME:-.}"/bin/send_broadcast.sh --help
exit
;;
@ -1299,10 +1247,10 @@ if [ -z "${SOURCE}" ]; then
SESSION="${ME:-_bot}-startbot"
BOTPID="$(proclist "${SESSION}")"
if [ -n "${BOTPID}" ]; then
echo -e "${GREEN}Bot is running with UID ${RUNUSER}.${NC}"
printf "${GREEN}Bot is running with UID ${RUNUSER}.${NN}"
exit
else
echo -e "${ORANGE}No Bot running with UID ${RUNUSER}.${NC}"
printf "${ORANGE}No Bot running with UID ${RUNUSER}.${NN}"
exit 5
fi
debug_checks "end status" "$@"
@ -1319,9 +1267,9 @@ if [ -z "${SOURCE}" ]; then
printf "Session Name: %s\n" "${SESSION}"
sleep 1
if [ -n "$(proclist "${SESSION}")" ]; then
echo -e "${GREEN}Bot started successfully.${NC}"
printf "${GREEN}Bot started successfully.${NN}"
else
echo -e "${RED}An error occurred while starting the bot.${NC}"
printf "${RED}An error occurred while starting the bot.${NN}"
exit 5
fi
debug_checks "end start" "$@"
@ -1335,36 +1283,35 @@ if [ -z "${SOURCE}" ]; then
# shellcheck disable=SC2086
if kill ${BOTPID}; then
# inform botadmin about stop
ADMIN="$(getConfigKey "botadmin")"
[ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot ${ME} stopped ..." &
echo -e "${GREEN}OK. Bot stopped successfully.${NC}"
send_normal_message "$(getConfigKey "botadmin")" "Bot ${ME} stopped ..." &
printf "${GREEN}OK. Bot stopped successfully.${NN}"
else
echo -e "${RED}An error occurred while stopping bot.${NC}"
printf "${RED}An error occurred while stopping bot.${NN}"
exit 5
fi
else
echo -e "${ORANGE}No Bot running with UID ${RUNUSER}.${NC}"
printf "${ORANGE}No Bot running with UID ${RUNUSER}.${NN}"
fi
debug_checks "end stop" "$@"
exit
;;
# suspend, resume or kill background jobs
"suspendb"*|"resumeb"*|"killb"*)
_is_function job_control || { echo -e "${RED}Module background is not available!${NC}"; exit 3; }
_is_function job_control || { printf "${RED}Module background is not available!${NN}"; exit 3; }
ME="$(getConfigKey "botname")"
job_control "$1"
debug_checks "end background $1" "$@"
;;
*)
echo -e "${RED}${REALME##*/}: unknown command${NC}"
echo -e "${RED}Available commands: ${GREY}${BOTCOMMANDS}${NC}" && exit
printf "${RED}${REALME##*/}: unknown command${NN}"
printf "${RED}Available commands: ${GREY}${BOTCOMMANDS}${NN}" && exit
exit 4
;;
esac
# warn if root
if [[ "${UID}" -eq "0" ]] ; then
echo -e "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}"
echo -e "${ORANGE}You are at HIGH RISK when running a Telegram BOT with root privileges!${NC}"
printf "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NN}"
printf "${ORANGE}You are at HIGH RISK when running a Telegram BOT with root privileges!${NN}"
fi
fi # end source

View File

@ -13,7 +13,7 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 18.12.2020 12:27
#
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-2-gde31231
#===============================================================================
############
@ -36,15 +36,8 @@ BASHBOT_ETC="${BASHBOT_HOME}"
if [ ! -r "${BASHBOT_HOME}/bashbot.sh" ]; then
echo "Bashbot.sh not found in \"${BASHBOT_HOME}\""
exit 4
else
# shellcheck disable=SC1090
source "${BASHBOT_HOME}/bashbot.sh" source "$1"
fi
# overwrite bot FILE regex to BASHBOT_ETC
# change this to the location you want to allow file uploads from
FILE_REGEX="${BASHBOT_ETC%/bin*}/.*"
# check for botconfig.jssh readable
if [ ! -r "${BASHBOT_ETC}/botconfig.jssh" ]; then
echo "Bashbot config file in \"${BASHBOT_ETC}\" does not exist or is not readable."
@ -56,6 +49,14 @@ if [ ! -r "${BASHBOT_VAR}/count.jssh" ]; then
exit 3
fi
# shellcheck disable=SC1090
source "${BASHBOT_HOME}/bashbot.sh" source "$1"
# overwrite bot FILE regex to BASHBOT_VAR
# change this to the location you want to allow file uploads from
UPLOADDIR="${BASHBOT_VAR%/bin*}"
FILE_REGEX="${UPLOADDIR}/.*"
# get and check ADMIN and NAME
BOT_ADMIN="$(getConfigKey "botadmin")"
BOT_NAME="$(getConfigKey "botname")"

View File

@ -24,8 +24,13 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 16.12.2020 16:14
#
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.20-3-g232a16b
#===============================================================================
# shellcheck disable=SC2059
####
# minimum messages seen in a chat before send a broadcast to it
MINCOUNT=2
####
# broadcast is dangerous, without --doit we do a dry run ...
@ -63,10 +68,10 @@ case "$1" in
shift
;;
'')
echo "missing missing arguments"
printf "missing missing arguments\n"
;&
"-h"*)
echo 'usage: send_message [-h|--help] [--groups|--both] [format] "message ...." [debug]'
printf 'usage: send_message [-h|--help] [--groups|--both] [format] "message ...." [debug]\n'
exit 1
;;
'--h'*)
@ -84,14 +89,14 @@ source "${0%/*}/bashbot_env.inc.sh" "$2" # $3 debug
declare -A SENDALL
jssh_readDB_async "SENDALL" "${COUNTFILE}"
if [ -z "${SENDALL[*]}" ]; then
echo -e "${ORANGE}Countfile not found or empty,${NC} "
printf "${ORANGE}Countfile not found or empty,${NC}\n"
fi
# loop over users
echo -e "${GREEN}Sending broadcast message to all users of ${BOT_NAME}${NC}${GREY}\c"
printf "${GREEN}Sending broadcast message to all users of ${BOT_NAME}${NC}${GREY}"
{ # dry run
[ -z "${DOIT}" ] && echo -e "${NC}\n${ORANGE}DRY RUN! use --doit as first argument to execute broadcast...${NC}"
[ -z "${DOIT}" ] && printf "${NC}\n${ORANGE}DRY RUN! use --doit as first argument to execute broadcast...${NC}\n"
for USER in ${!SENDALL[*]}
do
@ -100,14 +105,25 @@ fi
[[ "${SENDTO}" != "users" && "${USER}" != *"-"* ]] && continue
# ignore everything not a user or group
[[ ! "${USER}" =~ ^[0-9-]*$ ]] && continue
# ignore chats with no count or lower MINCOUNT
[[ ! "${SENDALL[${USER}]}" =~ ^[0-9]*$ || "${SENDALL[${USER}]}" -lt "${MINCOUNT}" ]] && continue
(( COUNT++ ))
if [ -z "${DOIT}" ]; then
echo "${SEND}" "${USER}" "$1" "$2"
printf "${SEND} ${USER} $1 $2\n"
else
"${SEND}" "${USER}" "$1" "$2"
echo -e ".\c" 1>&2
printf "." 1>&2
# ups, kicked or banned ...
if [ "${BOTSENT[ERROR]}" = "403" ]; then
# remove chat from future broadcasts
jssh_insertKeyDB "${USER}" "${SENDALL[${USER}]} banned" "${COUNTFILE}"
printf "${ORANGE}Warning: bot banned from chat${NC} %s ${ORANGE}after${NC} %s ${ORANGE}commands${NC}\n"\
"${USER}" "${SENDALL[${USER}]}"
fi
sleep 0.1
fi
done
echo -e "${NC}\nMessage \"$1\" sent to ${COUNT} ${SENDTO}${GROUPSALSO}."
# printout final stats message
printf "${NC}\n${GREEN}Message${NC} $1 ${GREEN}sent to${NC} ${COUNT} ${GREEN}${SENDTO}${GROUPSALSO}.${NC}\n"
} | more

View File

@ -22,7 +22,7 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 23.12.2020 16:52
#
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-0-g2e878fd
#===============================================================================
####
@ -42,10 +42,10 @@ case "$1" in
shift
;;
'')
echo "missing arguments"
printf "missing arguments\n"
;&
"-h"*)
echo 'usage: send_edit_message [-h|--help] [format] "CHAT[ID]" "MESSAGE[ID]" "message ..." [debug]'
printf 'usage: send_edit_message [-h|--help] [format] "CHAT[ID]" "MESSAGE[ID]" "message ..." [debug]\n'
exit 1
;;
'--h'*)

View File

@ -21,19 +21,18 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 25.12.2020 20:24
#
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-0-g2e878fd
#===============================================================================
####
# parse args
SEND="upload_file"
case "$1" in
'')
echo "missing arguments"
printf "missing arguments\n"
;&
"-h"*)
echo 'usage: send_file [-h|--help] "CHAT[ID]" "file" "caption ...." [debug]'
printf 'usage: send_file [-h|--help] "CHAT[ID]" "file" "caption ...." [debug]\n'
exit 1
;;
'--h'*)

View File

@ -21,10 +21,9 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 16.12.2020 11:34
#
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-0-g2e878fd
#===============================================================================
####
# parse args
SEND="send_message"
@ -42,10 +41,10 @@ case "$1" in
shift
;;
'')
echo "missing arguments"
printf "missing arguments\n"
;&
"-h"*)
echo 'usage: send_message [-h|--help] [format] "CHAT[ID]" "message ...." [debug]'
printf 'usage: send_message [-h|--help] [format] "CHAT[ID]" "message ...." [debug]\n'
exit 1
;;
'--h'*)

View File

@ -15,7 +15,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-23-g1a0b51d
#
# adjust your language setting here, e.g. when run from other user or cron.
@ -114,7 +114,7 @@ if [ -z "${1}" ] || [[ "${1}" == *"debug"* ]];then
;;
'/start'*)
send_action "${CHAT[ID]}" "typing"
MYCOMMANDS="*Note*: No _mycommands.sh_ detected, copy _mycommands.dist_ or _mycommands.clean_."
MYCOMMANDS="*Note*: MISSING mycommands.sh: copy _mycommands.dist_ or _mycommands.clean_."
[ -r "${BASHBOT_ETC:-.}/mycommands.sh" ] && MYCOMMANDS="Place your commands and messages in _mycommands.sh_"
user_is_botadmin "${USER[ID]}" &&\
send_markdownv2_message "${CHAT[ID]}" "You are *BOTADMIN*.\n${MYCOMMANDS}"

View File

@ -1,7 +1,12 @@
#!/usr/bin/env bash
# this has to run once atfer git clone
# and every time we create new hooks
#### $$VERSION$$ v1.20-0-g2ab00a2
#############################################################
#
# File: dev/all-tests.sh
#
# Description: run all tests, exit after failed test
#
#### $$VERSION$$ v1.21-dev-46-gd13e95a
#############################################################
# magic to ensure that we're always inside the root of our application,
# no matter from which directory we'll run script
@ -12,6 +17,7 @@ else
echo "Sorry, no git repository $(pwd)" && exit 1
fi
##########################
# create test environment
TESTENV="/tmp/bashbot.test$$"
mkdir "${TESTENV}"
@ -21,6 +27,13 @@ cd "test" || exit 1
# delete possible config
rm -f "${TESTENV}/botconfig.jssh" "${TESTENV}/botacl" 2>/dev/null
# inject JSON.sh
mkdir "${TESTENV}/JSON.sh"
curl -sL "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" >"${TESTENV}/JSON.sh/JSON.sh"
chmod +x "${TESTENV}/JSON.sh/JSON.sh"
########################
#prepare and run tests
#set -e
fail=0
tests=0
@ -45,6 +58,8 @@ do
fi
done
###########################
# cleanup depending on test state
if [ "$fail" -eq 0 ]; then
/bin/echo -n 'SUCCESS '
exitcode=0
@ -56,6 +71,8 @@ else
find "${TESTENV}/"* ! -name '[a-z]-*' -delete
fi
#########################
# show test result and test logs
echo -e "${passed} / ${tests}\\n"
[ -d "${TESTENV}" ] && echo "Logfiles from run are in ${TESTENV}"

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-10-g9bfc27a
############
# NOTE: you MUST run install-hooks.sh again when updating this file!
@ -68,7 +68,7 @@ fi
if command -v codespell &>/dev/null; then
echo "Running codespell"
echo "............................"
codespell --skip="*.zip,*gz,*.log,*.html,*.txt,.git*,jsonDB-keyboard" -L "ba"
codespell -q 3 --skip="*.zip,*gz,*.log,*.html,*.txt,.git*,jsonDB-keyboard" -L "ba"
echo "if there are (to many) typo's shown, consider running:"
echo "codespell -i 3 -w --skip=\"*.log,*.html,*.txt,.git*,examples\" -L \"ba\""
else

35
dev/inject-json.sh Normal file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env bash
##############################################################
#
# File: inject-json.sh
#
# Description: download and prepare JSON.sh and JSON.awk
#
# Usage: source inject-json.sh
#
#### $$VERSION$$ v1.21-dev-22-ga3efcd2
##############################################################
# download JSON.sh
JSONSHFILE="JSON.sh/JSON.sh"
if [ ! -r "${JSONSHFILE}" ]; then
printf "Inject JSON.sh ... "
mkdir "JSON.sh" 2>/dev/null
curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh"
chmod +x "${JSONSHFILE}"
printf "Done!\n"
fi
# download JSON.awk
JSONSHFILE="JSON.sh/JSON.awk.dist"
if [ ! -r "${JSONSHFILE}" ]; then
printf "Inject JSON.awk ... "
curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/step-/JSON.awk/JSON.awk"
curl -sL -o "${JSONSHFILE%/*}/awk-patch.sh" "https://cdn.jsdelivr.net/gh/step-/JSON.awk/tool/patch-for-busybox-awk.sh"
chmod +x "${JSONSHFILE}"
printf "Done!\n"
bash "${JSONSHFILE%/*}/awk-patch.sh" "${JSONSHFILE}"
fi
# delete backup files
rm -f "${JSONSHFILE%/*}"/*.bak

View File

@ -1,8 +1,14 @@
#!/usr/bin/env bash
# file: make-distribution.sh
# creates files and arcchives to dirtribute bashbot
##############################################################
#
#### $$VERSION$$ v1.20-0-g2ab00a2
# File: make-distribution.sh
#
# Description: creates files and arcchives to distribute bashbot
#
# Options: --notest - skip tests
#
#### $$VERSION$$ v1.21-dev-36-gc6001c2
##############################################################
# magic to ensure that we're always inside the root of our application,
# no matter from which directory we'll run script
@ -19,11 +25,12 @@ VERSION="$(git describe --tags | sed -e 's/-[0-9].*//' -e 's/v//')"
DISTNAME="telegram-bot-bash"
DISTDIR="./DIST/${DISTNAME}"
DISTFILES="bashbot.rc bashbot.sh commands.sh mycommands.sh mycommands.sh.clean bin doc examples scripts modules addons LICENSE README.md README.txt README.html"
DISTMKDIR="data-bot-bash logs"
# run tests first!
for test in dev/all-test*.sh
for test in $1 dev/all-test*.sh
do
[[ "${test}" == "--notest"* ]] && break
[ ! -x "${test}" ] && continue
if ! "${test}" ; then
echo "Test ${test} failed, can't create dist!"
@ -33,12 +40,18 @@ done
# create dir for distribution and copy files
mkdir -p "${DISTDIR}" 2>/dev/null
# shellcheck disable=SC2086
echo "Copy files"
# shellcheck disable=SC2086
cp -r ${DISTFILES} "${DISTDIR}"
cd "${DISTDIR}" || exit 1
echo "Create directories"
for dir in $DISTMKDIR
do
[ ! -d "${dir}" ] && mkdir "${dir}"
done
# do not overwrite on update
echo "Create .dist files"
for file in mycommands.sh bashbot.rc addons/*.sh
@ -47,22 +60,9 @@ do
mv "${file}" "${file}.dist"
done
# dwonload JSON.sh
echo "Inject JSON.sh"
JSONSHFILE="JSON.sh/JSON.sh"
if [ ! -r "${JSONSHFILE}" ]; then
mkdir "JSON.sh" 2>/dev/null
curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh"
chmod +x "${JSONSHFILE}"
fi
echo "Inject JSON.awk"
JSONSHFILE="JSON.sh/JSON.awk"
if [ ! -r "${JSONSHFILE}" ]; then
curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/step-/JSON.awk/JSON.awk"
curl -sL -o "${JSONSHFILE%/*}/awk-patch.sh" "https://cdn.jsdelivr.net/gh/step-/JSON.awk/tool/patch-for-busybox-awk.sh"
bash "${JSONSHFILE%/*}/awk-patch.sh" "${JSONSHFILE%/*}/JSON.awk"
fi
rm -f "${JSONSHFILE%/*}"/*.bak
# inject JSON.sh into distribution
# shellcheck disable=SC1090
source "$GIT_DIR/../dev/inject-json.sh"
# make html doc
echo "Create html doc"

View File

@ -1,7 +1,14 @@
data-bot-bash/*
*.jssh
*.jssh*
botacl
*.flock
*.log
*.last
*.ok
*.bad
*.bak
*.jpg
*.jpeg
*.png
*.zip
*.gz

View File

@ -1,11 +1,16 @@
#!/usr/bin/env bash
# file: make-standalone.sh
# even after make-distribution.sh bashbot is not self contained as it was in the past.
###################################################################
#
# If you your bot is finished you can use make-standalone.sh to create the
# the old all-in-one bashbot: bashbot.sh and commands.sh only!
# File: make-standalone.sh
#
#### $$VERSION$$ v1.20-0-g2ab00a2
# Description:
# even after make-distribution.sh bashbot is not self contained as it was in the past.
#
# If you your bot is finished you can use make-standalone.sh to create the
# the old all-in-one bashbot: bashbot.sh and commands.sh only!
#
#### $$VERSION$$ v1.21-dev-17-g8c9298d
###################################################################
# magic to ensure that we're always inside the root of our application,
# no matter from which directory we'll run script
@ -30,6 +35,10 @@ mkdir -p "${DISTDIR}" 2>/dev/null
cp -r ${DISTFILES} "${DISTDIR}" 2>/dev/null
cd "${DISTDIR}" || exit 1
# inject JSON.sh into distribution
# shellcheck disable=SC1090
source "$GIT_DIR/../dev/inject-json.sh"
#######################
# here the magic starts
# create all in one bashbot.sh file

View File

@ -3,6 +3,8 @@
## Check bash installation
There may systems where bash seems to be installed but it is not, e.g. embedded systems, or where bash is to old.
Bashbot has some builtin checks but it may better to check before installing bashbot.
Run the following commands to see if your bash looks ok ...
```bash
@ -13,7 +15,10 @@ if which bash; then echo "bash seems available..."; else echo "NO bash"; fi
bash -c 'if eval "a[1]=1"; then echo "Shell support arrays..."; else echo "Shell has NO arrays"; fi'
# check for bash version by feature
bash -c 'if [ "$(echo -e "\u1111")" != "\u1111" ]; then echo "Bash version ok ..."; else echo "LBash Version may be to old ..."; fi'
bash -c 'if [ "$(echo -e "\u1111")" != "\u1111" ]; then echo "Bash version ok ..."; else echo "Bash version may to old ..."; fi'
# display bash version, must be greater than 4.3
bash --version | grep "bash"
```
## Install bashbot
@ -21,12 +26,12 @@ bash -c 'if [ "$(echo -e "\u1111")" != "\u1111" ]; then echo "Bash version ok ..
1. Go to the directory you want to install bashbot, e.g.
* your $HOME directory (install and run with your user-ID)
* /usr/local if you want to run as service
2. [Download latest release zip / tar archive from github](https://github.com/topkecleon/telegram-bot-bash/releases) and extract all files.
3. Change into the directory ```telegram-bot-bash```
4. Activate the bot example commands ```cp mycommands.sh.dist mycommands.sh```
5. Run ```./bashbot.sh init``` to setup the environment and enter your Bots token given by botfather.
2. [Download latest release zip / tar archive from github](https://github.com/topkecleon/telegram-bot-bash/releases/latest) and extract all files.
3. Change into the directory `telegram-bot-bash`
4. Copy `mycommands.sh.dist` or `mycommands.sh.clean` to `mycommands.sh`
5. Run `./bashbot.sh init` to setup the environment and enter your Bots token given by botfather.
Edit ```mycommands.sh``` to your needs.
Edit `mycommands.sh` to fit your needs.
Now your Bot is ready to start ...
**If you are new to Bot development read [Bots: An introduction for developers](https://core.telegram.org/bots)**
@ -34,25 +39,21 @@ Now your Bot is ready to start ...
### Update bashbot
**Note: all files including ```mycommands.sh``` may overwritten, make a backup!**
**Important: all files including `mycommands.sh` may overwritten, make a backup!**
1. Go to the directory where you had installed bashbot, e.g.
1. Go to the directory where you have installed bashbot, e.g.
* your $HOME directory
* /usr/local
2. [Download latest release zip / tar archive from github](https://github.com/topkecleon/telegram-bot-bash/releases)
3. Stop all running instances of bashbot
2. [Download latest release zip / tar archive from github](https://github.com/topkecleon/telegram-bot-bash/releases/latest)
3. Stop all running instances of bashbot `./bashbot.sh stop`
4. Extract all files to your existing bashbot dir
5. Run ```sudo ./bashbot.sh init``` to setup your environment after the update
5. Run `sudo ./bashbot.sh init` to setup your environment after the update
If you modified ```commands.sh``` move your changes to ```mycommands.sh```, this avoids overwriting your commands on update.
If you modified `commands.sh` move your changes to `mycommands.sh`, this avoids overwriting your commands on every update.
Now you can restart your bashbot instances.
### Get zip / tar archive on command line
```bash
wget -q https://github.com/$(wget -q https://github.com/topkecleon/telegram-bot-bash/releases/latest -O - | egrep '/.*/.*/.*tar.gz' -o)
```
*Note*: If you are updating from a pre-1.0 version, update to [Version 1.20](https://github.com/topkecleon/telegram-bot-bash/releases/tags/v1.20) first!
### Use JSON.awk (beta)
@ -62,7 +63,7 @@ Most systems with awk can use `JSON.awk` as drop in replacement
BSD and MacOS users must install `gnu awk` and adjust the shebang, see below
After you have checked that 'JSON.awk' is working correct on your system make `JSON.awk` executable and (re)start bashbot.
After you have checked that 'JSON.awk.dist' is working correct on your system copy it to `JSON.awk` and (re)start bashbot.
Note: If you are not using the zip / tar archive, you must install `JSON.awk` manually into the same directory as 'JSON.sh`:
@ -71,30 +72,30 @@ Note: If you are not using the zip / tar archive, you must install `JSON.awk` ma
bash patch-for-busybox-awk.sh
### Install bashbot from github
### Install bashbot from git repo
The first install can also be done from github, updates should always done from the zip or tar archive to avoid
overwriting your config and ```mycommands.sh``` files.
Installation and Updates should be done using the zip / tar archives provided on github to avoid
problems and not overwriting your bot config and `mycommands.sh`.
1. Go to the directory you want to install bashbot, e.g.
* your $HOME directory (install and run with your user-ID)
* /usr/local if you want to run as service
2. Run ```git clone https://github.com/topkecleon/telegram-bot-bash.git```
3. Change into the directory ```telegram-bot-bash```
4. Run ``` dev/all-tests.sh``` and if everything finish OK ...
5. Run ``` rm addons/*``` to deactivate the example add on's
5. Run ```sudo ./bashbot.sh init``` to setup the environment and enter your Bots token given by botfather.
Nevertheless you can install or update bashbot from a git repo, see next chapter ...
### Get the latest development updates from github
### Create Installation / Update archives
To update an existing installation to the latest development version from github you must create the update archives yourself.
To install or update bashbot from git repo execute `dev/make-distribution.sh`, this creates the archives and set up bashbot to run in `DIST/telegram.bot-bash`.
1. Run ```git clone https://github.com/topkecleon/telegram-bot-bash.git```
2. Change into the directory ```telegram-bot-bash```
3. Run ``` git checkout develop```
4. Run ``` dev/make-distribution.sh```
5. If no errors occur you will find archive files in DISTRIBUTION
*Note:* You should be familiar with `git`.
1. Run `git clone https://github.com/topkecleon/telegram-bot-bash.git`
2. Change into the directory `telegram-bot-bash`
3. Optional: Run ` git checkout develop` for latest develop version
4. Run ` dev/make-distribution.sh` (_add --notest to skip tests_)
5. Change to dir `DIST/`
Use the archives created in `DIST/` to install or update bashbot as described above.
To run a test bot, e.g. while development or testing latest changes, you can use the bashbot installation provided in `DIST/telegram-bot-bash`.
To update the test installation, e.g. after git pull, local changes or switch master/develop, run `dev/make-distrubition.sh` again.
### Note for BSD and MacOS
@ -113,77 +114,22 @@ Bashbot will stay with /bin/bash shebang, as using a fixed path is more secure t
I considered to make bashbot BSD sed compatible, but much of the bashbot "magic" relies on
(gnu) sed features, e.g. alternation ```|```, non printables ```\n\t\<``` or repeat ```?+``` pattern, not supported by BSD sed.
BSD/MacOS sed compatibility will result in a rewrite of all grep/sed commands with an uncertain outcome,
see [BSD/MacOS vs. GNU sed](https://riptutorial.com/sed/topic/9436/bsd-macos-sed-vs--gnu-sed-vs--the-posix-sed-specification)
to get an impression how different they are.
If you can convert the following examples to work correct with gnu and BSD sed, contact me.
```bash
# easy start
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p'
OUT="$(sed -e ':a;N;$!ba;s/\r\n/ mynewlinestartshere /g' <<<"$1"| iconv -f utf-8 -t utf-8 -c)"
### Notes on Changes
# more complex
address="$(sed <<< "${2}" '/myaddressstartshere /!d;s/.*myaddressstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"
#### Support for update from pre-1.0 removed
# for experts?
source <( printf "$1"'=( %s )' "$(sed -E -n -e ':x /"text"\]/ { N; s/([^"])\n/\1\\n/g ; tx }' -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" )
```
### Notes per Version
#### Change in storing config values
Up to version 0.94 bashbot stores config values as values in ```token```, ```botadmin``` and ```count```. Since version 0.96 bashbot
uses jsonDB key/value store. Config is stored in ```botconfig.jssh```, counting of users is done in ```count.jssh```.
The acl file ```botacl``` stay as is. On first run of bashbot.sh after an update bashbot converts
the files to the new config format. Afterwards the files ```token```, ```botadmin``` and ```count``` can be deleted.
You may notice the new file ```blocked.jssh```, every telegram user or chat id stored here will be blocked from
using your bot.
#### removal of TMUX
From version 0.80 on TMUX is no longer needed and the bashbot command 'attach' is deleted. Old function 'inproc'
is replaced by 'send_interactive'. send_interactive does checks if an interactive job is running internally.
Pls check if you make use of inproc and remove it including the old checks, e.g.
```bash
if tmux ls | grep -v send | grep -q "$copname"; then inproc; fi
# or
[ checkprog ] && inproc
```
must be replaced by ```send_interactive "${CHATD[ID]}" "${MESSAGE}"```
### Do not edit commands.sh
From version 0.60 on your commands must be placed in ```mycommands.sh```. If you update from a version with your commands
in 'commands.sh' move all your commands and functions to ```mycommands.sh```.
From version 0.80 on 'commands.sh' will be overwritten on update!
#### Location of var / tmp / data dirs
From version 0.70 on the tmp dir is renamed to 'data-bot-bash' to reflect the fact that not only temporary files are stored. an existing 'tmp-bot-bash' will be automatically renamed after update.
From version 0.50 on the temporary files are no more placed in '/tmp'. instead a dedicated tmp dir is used.
#### Changes to send_keyboard in v0.6
From Version 0.60 on keyboard format for ```send_keyboard``` and ```send_message "mykeyboardstartshere ..."``` was changed.
Keyboards are now defined in JSON Array notation e.g. "[ \\"yes\\" , \\"no\\" ]".
This has the advantage that you can create any type of keyboard supported by Telegram.
The old format is supported for backward compatibility, but may fail for corner cases.
*Example Keyboards*:
- yes no in two rows:
- OLD format: 'yes' 'no' *(two strings)*
- NEW format: '[ "yes" ] , [ "no" ]' *(two arrays with a string)*
- new layouts made easy with NEW format:
- Yes No in one row: '[ "yes" , "no" ]'
- Yes No plus Maybe in 2.row: '[ "yes" , "no" ] , [ "maybe" ]'
- numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]'
From Version 1.21 on updating from a pre-1.0 version is no more supported!
You must update to [Version 1.20](https://github.com/topkecleon/telegram-bot-bash/releases/tags/v1.20) first!
#### [Next Create Bot](1_firstbot.md)
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-41-g41378f8

View File

@ -1,14 +1,15 @@
#### [Home](../README.md)
## Getting Started
The Bots standard commands are in the commands dispatcher ```commands.sh```, Do not edit this file! Add your commands and functions to ```mycommands.sh```. In 'mycommands.sh.dist' you find examples how to add own commands and overwrite existing ones. See [Best practices](5_practice.md) for more information.
The Bots default commands are in `commands.sh`. Do not edit this file! Instead copy `mycommands.sh.clean` to `mycommands.sh` and place you commands there.
Have a look at `mycommands.sh.dist` for examples on how to write commands or overwrite existing ones. See [Best practices](5_practice.md) for more information.
Once you're done with editing start the Bot with ```./bashbot.sh start```. To stop the Bot run ```./bashbot.sh stop```
Once you're done with editing run the Bot with `./bashbot.sh start`. To stop running the Bot use `./bashbot.sh stop`
If something doesn't work as expected, debug with ```./bashbot.sh startbot DEBUG &```, where DEBUG can be 'debug', 'xdebug' or 'xdebugx'.
If something doesn't work as expected, debug with `./bashbot.sh startbot DEBUG &`, where DEBUG can be 'debug', 'xdebug' or 'xdebugx'.
See [Bashbot Development](7_develop.md) for more information.
To use the functions provided in this script in other scripts simply source bashbot: ```source bashbot.sh source```. see [Expert Use](8_expert.md#Expert-use)
To use the functions provided in this script in other scripts simply source bashbot: `source bashbot.sh source`. see [Expert Use](8_expert.md#Expert-use)
Have FUN!
@ -143,60 +144,60 @@ Evertime a Message is received, you can read incoming data using the following v
These Variables are always present in regular messages:
* ```${MESSAGE}```: Current message text
* ```${MESSAGE[ID]}```: ID of current message
* ```$USER```: This array contains the First name, last name, username and user id of the sender of the current message.
* ```${USER[ID]}```: User id
* ```${USER[FIRST_NAME]}```: User's first name
* ```${USER[LAST_NAME]}```: User's last name
* ```${USER[USERNAME]}```: Username
* ```$CHAT```: This array contains the First name, last name, username, title and user id of the chat of the current message.
* ```${CHAT[ID]}```: Chat id
* ```${CHAT[FIRST_NAME]}```: Chat's first name
* ```${CHAT[LAST_NAME]}```: Chat's last name
* ```${CHAT[USERNAME]}```: Username
* ```${CHAT[TITLE]}```: Title
* ```${CHAT[TYPE]}```: Type
* ```${CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]}```: All members are administrators (true if true)
* `${MESSAGE}`: Current message text
* `${MESSAGE[ID]}`: ID of current message
* `$USER`: This array contains the First name, last name, username and user id of the sender of the current message.
* `${USER[ID]}`: User id
* `${USER[FIRST_NAME]}`: User's first name
* `${USER[LAST_NAME]}`: User's last name
* `${USER[USERNAME]}`: Username
* `$CHAT`: This array contains the First name, last name, username, title and user id of the chat of the current message.
* `${CHAT[ID]}`: Chat id
* `${CHAT[FIRST_NAME]}`: Chat's first name
* `${CHAT[LAST_NAME]}`: Chat's last name
* `${CHAT[USERNAME]}`: Username
* `${CHAT[TITLE]}`: Title
* `${CHAT[TYPE]}`: Type
* `${CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]}`: All members are administrators (true if true)
The following variables are set if the message contains optional parts:
* ```$REPLYTO```: Original message which was replied to
* ```$REPLYTO```: This array contains the First name, last name, username and user id of the ORIGINAL sender of the message REPLIED to.
* ```${REPLYTO[ID]}```: ID of message which was replied to
* ```${REPLYTO[UID]}```: Original user's id
* ```${REPLYTO[FIRST_NAME]}```: Original user's first name
* ```${REPLYTO[LAST_NAME]}```: Original user's' last name
* ```${REPLYTO[USERNAME]}```: Original user's username
* ```$FORWARD```: This array contains the First name, last name, username and user id of the ORIGINAL sender of the FORWARDED message.
* ```${FORWARD[ID]}```: Same as MESSAGE[ID] if message is forwarded
* ```${FORWARD[UID]}```: Original user's id
* ```${FORWARD[FIRST_NAME]}```: Original user's first name
* ```${FORWARD[LAST_NAME]}```: Original user's' last name
* ```${FORWARD[USERNAME]}```: Original user's username
* ```$CAPTION```: Picture, Audio, Video, File Captions
* ```$URLS```: This array contains documents, audio files, voice recordings and stickers as URL.
* ```${URLS[AUDIO]}```: Audio files
* ```${URLS[VIDEO]}```: Videos
* ```${URLS[PHOTO]}```: Photos (maximum quality)
* ```${URLS[VOICE]}```: Voice recordings
* ```${URLS[STICKER]}```: Stickers
* ```${URLS[DOCUMENT]}```: Any other file
* ```$CONTACT```: This array contains info about contacts sent in a chat.
* ```${CONTACT[ID]}```: User id
* ```${CONTACT[NUMBER]}```: Phone number
* ```${CONTACT[FIRST_NAME]}```: First name
* ```${CONTACT[LAST_NAME]}```: Last name
* ```${CONTACT[VCARD]}```: User's complete Vcard
* ```$LOCATION```: This array contains info about locations sent in a chat.
* ```${LOCATION[LONGITUDE]}```: Longitude
* ```${LOCATION[LATITUDE]}```: Latitude
* ```$VENUE```: This array contains info about venue (a place) sent in a chat.
* ```${VENUE[TITLE]}```: Name of the place
* ```${VENUE[ADDRESS]}```: Address of the place
* ```${VENUE[LONGITUDE]}```: Longitude
* ```${VENUE[LATITUDE]}```: Latitude
* ```${VENUE[FOURSQUARE]}```: Fouresquare ID
* `$REPLYTO`: Original message which was replied to
* `$REPLYTO`: This array contains the First name, last name, username and user id of the ORIGINAL sender of the message REPLIED to.
* `${REPLYTO[ID]}`: ID of message which was replied to
* `${REPLYTO[UID]}`: Original user's id
* `${REPLYTO[FIRST_NAME]}`: Original user's first name
* `${REPLYTO[LAST_NAME]}`: Original user's' last name
* `${REPLYTO[USERNAME]}`: Original user's username
* `$FORWARD`: This array contains the First name, last name, username and user id of the ORIGINAL sender of the FORWARDED message.
* `${FORWARD[ID]}`: Same as MESSAGE[ID] if message is forwarded
* `${FORWARD[UID]}`: Original user's id
* `${FORWARD[FIRST_NAME]}`: Original user's first name
* `${FORWARD[LAST_NAME]}`: Original user's' last name
* `${FORWARD[USERNAME]}`: Original user's username
* `$CAPTION`: Picture, Audio, Video, File Captions
* `$URLS`: This array contains documents, audio files, voice recordings and stickers as URL.
* `${URLS[AUDIO]}`: Audio files
* `${URLS[VIDEO]}`: Videos
* `${URLS[PHOTO]}`: Photos (maximum quality)
* `${URLS[VOICE]}`: Voice recordings
* `${URLS[STICKER]}`: Stickers
* `${URLS[DOCUMENT]}`: Any other file
* `$CONTACT`: This array contains info about contacts sent in a chat.
* `${CONTACT[ID]}`: User id
* `${CONTACT[NUMBER]}`: Phone number
* `${CONTACT[FIRST_NAME]}`: First name
* `${CONTACT[LAST_NAME]}`: Last name
* `${CONTACT[VCARD]}`: User's complete Vcard
* `$LOCATION`: This array contains info about locations sent in a chat.
* `${LOCATION[LONGITUDE]}`: Longitude
* `${LOCATION[LATITUDE]}`: Latitude
* `$VENUE`: This array contains info about venue (a place) sent in a chat.
* `${VENUE[TITLE]}`: Name of the place
* `${VENUE[ADDRESS]}`: Address of the place
* `${VENUE[LONGITUDE]}`: Longitude
* `${VENUE[LATITUDE]}`: Latitude
* `${VENUE[FOURSQUARE]}`: Fouresquare ID
### Service Messages
@ -207,34 +208,34 @@ client, e.g. new users.
If a service message is received bashbot sets MESSAGE to the service message type as a command,
e.g. if a new user joins a chat MESSAGE is set to "/_new_chat_user".
* ```$SERVICE```: This array contains info about received service messages.
* ```${SERVICE}```: "yes" if service message is received
* ```${SERVICE[NEWMEMBER]}}```: New user's id
* ```${MESSAGE}```: /_new_chat_member ID NAME
* ```${NEWMEMBER[ID]}```: New user's id
* ```${NEWMEMBER[FIRST_NAME]}```: New user's first name
* ```${NEWMEMBER[LAST_NAME]}```: New user's last name
* ```${NEWMEMBER[USERNAME]}```: New user's username
* ```${NEWMEMBER[ISBOT]}```: New user is a bot
* ```${SERVICE[LEFTMEMBER]}```: Id of user left
* ```${MESSAGE}```: /_left_chat_member ID NAME
* ```${LEFTMEMBER[ID]}```: Left user's id
* ```${LEFTMEMBER[FIRST_NAME]}```: Left user's first name
* ```${LEFTMEMBER[LAST_NAME]}```: Left user's last name
* ```${LEFTMEMBER[USERNAME]}```: Left user's username
* ```${LEFTMEMBER[ISBOT]}```: Left user is a bot
* ```${SERVICE[NEWTITLE]}```: Text of new title
* ```${MESSAGE}```: /_new_chat_title SENDER TEXT
* ```${SERVICE[NEWPHOTO]}```: New Chat Picture
* ```${MESSAGE}```: /_new_chat_picture SENDER URL
* ```${SERVICE[PINNED]}```: Pinned MESSAGE ID
* ```${MESSAGE}```: /_new_pinned_message SENDER ID
* ```${PINNED[ID]}```: Id of pinned message
* ```${PINNED[MESSAGE]}```: Message text of pinned message
* ```${SERVICE[MIGRATE]}```: Old and new group id
* ```${MESSAGE}```: /_migrate_group MIGRATE_FROM MIGRATE_TO
* ```${MIGRATE[FROM]}```: Old group id
* ```${MIGRATE[TO]}```: New group id
* `$SERVICE`: This array contains info about received service messages.
* `${SERVICE}`: "yes" if service message is received
* `${SERVICE[NEWMEMBER]}}`: New user's id
* `${MESSAGE}`: /_new_chat_member ID NAME
* `${NEWMEMBER[ID]}`: New user's id
* `${NEWMEMBER[FIRST_NAME]}`: New user's first name
* `${NEWMEMBER[LAST_NAME]}`: New user's last name
* `${NEWMEMBER[USERNAME]}`: New user's username
* `${NEWMEMBER[ISBOT]}`: New user is a bot
* `${SERVICE[LEFTMEMBER]}`: Id of user left
* `${MESSAGE}`: /_left_chat_member ID NAME
* `${LEFTMEMBER[ID]}`: Left user's id
* `${LEFTMEMBER[FIRST_NAME]}`: Left user's first name
* `${LEFTMEMBER[LAST_NAME]}`: Left user's last name
* `${LEFTMEMBER[USERNAME]}`: Left user's username
* `${LEFTMEMBER[ISBOT]}`: Left user is a bot
* `${SERVICE[NEWTITLE]}`: Text of new title
* `${MESSAGE}`: /_new_chat_title SENDER TEXT
* `${SERVICE[NEWPHOTO]}`: New Chat Picture
* `${MESSAGE}`: /_new_chat_picture SENDER URL
* `${SERVICE[PINNED]}`: Pinned MESSAGE ID
* `${MESSAGE}`: /_new_pinned_message SENDER ID
* `${PINNED[ID]}`: Id of pinned message
* `${PINNED[MESSAGE]}`: Message text of pinned message
* `${SERVICE[MIGRATE]}`: Old and new group id
* `${MESSAGE}`: /_migrate_group MIGRATE_FROM MIGRATE_TO
* `${MIGRATE[FROM]}`: Old group id
* `${MIGRATE[TO]}`: New group id
@ -243,12 +244,12 @@ e.g. if a new user joins a chat MESSAGE is set to "/_new_chat_user".
Inline query messages are small, non regular messages used for interaction with the user,
they contain the following variables only:
* ```${iQUERY}```: Current inline query
* ```$iQUERY```: This array contains the ID, First name, last name, username and user id of the sender of the current inline query.
* ```${iQUERY[ID]}```: Inline query ID
* ```${iQUERY[USER_ID]}```: User's id
* ```${iQUERY[FIRST_NAME]}```: User's first name
* ```${iQUERY[LAST_NAME]}```: User's last name
* `${iQUERY}`: Current inline query
* `$iQUERY`: This array contains the ID, First name, last name, username and user id of the sender of the current inline query.
* `${iQUERY[ID]}`: Inline query ID
* `${iQUERY[USER_ID]}`: User's id
* `${iQUERY[FIRST_NAME]}`: User's first name
* `${iQUERY[LAST_NAME]}`: User's last name
### Send Message Results
@ -256,15 +257,15 @@ they contain the following variables only:
BOTSENT is set on every send_xxx action and only valid until next send action. For more on message results see.
[Advanced Usage](3_advanced.md)
* ```$BOTSENT```: This array contains the parsed results from the last transmission to telegram.
* ```${BOTSENT[OK]}```: contains the string ```true```: after a successful transmission
* ```${BOTSENT[ID]}```: Message ID of sent message, image, file etc., if OK is true
* `$BOTSENT`: This array contains the parsed results from the last transmission to telegram.
* `${BOTSENT[OK]}`: contains the string `true`: after a successful transmission
* `${BOTSENT[ID]}`: Message ID of sent message, image, file etc., if OK is true
## Usage of bashbot functions
#### sending messages
To send messages use the ```send_xxx_message``` functions.
To send messages use the `send_xxx_message`functions.
To insert line brakes in a message place `\n` in the text.
To send regular text without any markdown use:
@ -280,7 +281,7 @@ To send text with html:
send_html_message "${CHAT[ID]}" "lol <b>bold</b>"
```
To forward messages use the ```forward``` function:
To forward messages use the `forward`function:
```bash
forward "${CHAT[ID]}" "from_chat_id" "message_id"
```
@ -313,24 +314,24 @@ send_message "${CHAT[ID]}" "lol" "safe"
#### Send files, locations, keyboards.
To send images, videos, voice files, photos etc. use the ```send_photo``` function (remember to change the safety Regex @ line 14 of command.sh to allow sending files only from certain directories):
To send images, videos, voice files, photos etc. use the `send_photo`function (remember to change the safety Regex @ line 14 of command.sh to allow sending files only from certain directories):
```bash
send_file "${CHAT[ID]}" "/home/user/doge.jpg" "Lool"
```
To send custom keyboards use the ```send_keyboard``` function:
To send custom keyboards use the `send_keyboard`function:
```bash
send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" '[ "Yep" , "No" ]' # note the single quotes!
send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" "[ \\"Yep\\" , \\"No\\" ]" # within double quotes you must escape the inside double quots
```
To send locations use the ```send_location``` function:
To send locations use the `send_location`function:
```bash
send_location "${CHAT[ID]}" "Latitude" "Longitude"
```
To send venues use the ```send_venue``` function:
To send venues use the `send_venue`function:
```bash
send_venue "${CHAT[ID]}" "Latitude" "Longitude" "Title" "Address" "optional foursquare id"
```
To send a chat action use the ```send_action``` function.
To send a chat action use the `send_action`function.
Allowed values: typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for locations.
```bash
send_action "${CHAT[ID]}" "action"
@ -340,5 +341,5 @@ send_action "${CHAT[ID]}" "action"
#### [Prev Create Bot](1_firstbot.md)
#### [Next Advanced Usage](3_advanced.md)
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-28-g43f5536

View File

@ -2,7 +2,7 @@
## Advanced Features
### Access control
Bashbot offers functions to check what Telegram capabilities like 'chat admin' or 'chat creator' the given user has:
Bashbot offers functions to check what Telegram capabilities like `chat admin` or `chat creator` the given user has:
```bash
# return true if user is admin/owner of the bot
@ -21,7 +21,7 @@ user_is_admin "${CHAT[ID]}" "${USER[ID]}" && send_markdown_message "${CHAT[ID]}"
```
In addition you can check individual capabilities of users as you must define in the file ```./botacl```:
In addition you can check individual capabilities of users as you must define in the file `./botacl`:
```bash
# file: botacl
@ -54,7 +54,7 @@ ALL:*:*
```
You must use the function ```user_is_allowed``` to check if a user has the capability to do something. Example: Check if user has capability to start bot.
You must use the function `user_is_allowed` to check if a user has the capability to do something. Example: Check if user has capability to start bot.
```bash
case "$MESSAGE" in
@ -74,8 +74,8 @@ You must use the function ```user_is_allowed``` to check if a user has the capab
### Interactive Chats
Interactive chats are short running scripts, reading user input and echo data to the user.
To create a new interactive chat script copy 'scripts/interactive.sh.clean' to e.g. 'scripts/mynewinteractive.sh', make it executable
and then use 'start_proc' function from your bot, it's possible to pass two arguments. You find more examples for interactive scripts in 'examples'
To create a new interactive chat script copy `scripts/interactive.sh.clean` to e.g. `scripts/mynewinteractive.sh`, make it executable
and then use `start_proc` function from your bot, it's possible to pass two arguments. You find more examples for interactive scripts in 'examples'
*usage*: start_proc chat_id script arg1 arg2
@ -83,7 +83,7 @@ and then use 'start_proc' function from your bot, it's possible to pass two argu
*usage*: check_prog chat_id
**Note:** From Version 0.96 on scripts must read user input from '$3' instead of stdin!
**IMPORTANT:** Scripts must read user input from '$3' instead of stdin!
```bash
#!/bin/bash
@ -105,8 +105,8 @@ echo -e "Your Message: ${test}\nbye!"
#### message formatting and keyboards
The output of the script will be processed by 'send_messages', so you can not only send text, but also keyboards, files, locations and more.
Each newline in the output will start an new message to the user. To have line breaks in your message you must insert ' mynewlinestartshere ' or '\n' instead.
The output of the script will be processed by `send_messages`, so you can not only send text, but also keyboards, files, locations and more.
Each newline in the output will start an new message to the user. To have line breaks in your message you must insert `\n` instead.
To open up a keyboard in an interactive script, print out the keyboard layout in the following way:
```bash
@ -114,7 +114,7 @@ echo "Text that will appear in chat? mykeyboardstartshere [ \"Yep, sure\" , \"No
```
Same goes for files:
```bash
echo "Text that will appear in chat? myfilelocationstartshere /home/user/dog.jpg"
echo "Text that will appear in chat? myfilestartshere /home/user/dog.jpg"
```
*Note*: Use an _absolute path name_ (starting with `/`), a relative path name is relative to `data-bot-bash/upload`!
See [send_file documentation](6_reference.md#send_file) for more information.
@ -133,18 +133,17 @@ echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45 myti
```
You can combine them:
```bash
echo "Text that will appear in chat? mykeyboardstartshere [ \"Yep, sure\" , \"No, highly unlikely\" ] myfilelocationstartshere /home/user/doge.jpg mylatstartshere 45 mylongstartshere 45"
echo "Text that will appear in chat? mykeyboardstartshere [ \"Yep, sure\" , \"No, highly unlikely\" ] myfilestartshere /home/user/doge.jpg mylatstartshere 45 mylongstartshere 45"
```
Please note that you can either send a location or a venue, not both. To send a venue add the mytitlestartshere and the myaddressstartshere keywords.
To insert a line break in your message you can insert ` mynewlinestartshere ` or `\n` in your echo command:
To insert a line break in your message you can insert `\n` in your echo command:
```bash
echo "Text that will appear in one message mynewlinestartshere with this text on a new line"
echo "Other text message\nwith a newline" # \n instead of mynewlinestartshere
echo "Text that will appear in one message \nwith this text on a new line"
```
New in v0.7: In case you must extend a message already containing a location, a file, a keyboard etc.,
with additionial text simply add ``` mytextstartshere additional text``` at the end of the string:
In case you want extend a message already containing a location, a file, a keyboard etc.,
with an additionial text simply add ` mytextstartshere additional text`at the end of the string:
```bash
out="Text that will appear mylatstartshere 45 mylongstartshere 45"
[[ "$out" != *'in chat'* ]] && out="$out mytextstartshere in chat."
@ -183,14 +182,14 @@ If you want to kill all background jobs permanently run:
./bashbot.sh killback
```
Note: Background jobs run independent from main bot and continue running until your script exits or you stop it. Background jobs will continue running if your Bot is stopped and must be terminated separately e.g. by ```bashbot.sh killback```
Note: Background jobs run independent from main bot and continue running until your script exits or you stop it. Background jobs will continue running if your Bot is stopped and must be terminated separately e.g. by `bashbot.sh killback`
### Inline queries
**Inline queries** allow users to send commands to your bot from every chat without going to a private chat. An inline query is started if the user type the bots name, e.g. @myBot. Everything after @myBot is immediately send to the bot.
In order to enable **inline mode**, send `/setinline` command to [@BotFather](https://telegram.me/botfather) and provide the placeholder text that the user will see in the input field after typing your bots name.
The following commands allows you to send ansers to *inline queries*. To enable bashbot to process inline queries set ```INLINE="1"``` in 'mycommands.sh'.
The following commands allows you to send ansers to *inline queries*. To enable bashbot to process inline queries set `INLINE="1"`in 'mycommands.sh'.
To send messages or links through an *inline query*:
```bash
@ -246,19 +245,19 @@ is received.
**Note**: the values of the variables contains always the result of the LAST transmission to telegram,
every send action will overwrite them!
* ```$BOTSENT```: This array contains the parsed results from the last transmission to telegram.
* ```${BOTSENT[OK]}```: contains the string ```true```: after a successful transmission
* ```${BOTSENT[ID]}```: Message ID if OK is true
* ```${BOTSENT[ERROR]}```: Error code if an error occurred
* ```${BOTSENT[DESC]}```: Description text for error
* ```${BOTSENT[RETRY]}```: Seconds to wait if telegram requests throtteling.
* ```$res```: temporary variable containing the full transmission result, may be overwritten by any bashbot function.
* `$BOTSENT`: This array contains the parsed results from the last transmission to telegram.
* `${BOTSENT[OK]}`: contains the string `true`: after a successful transmission
* `${BOTSENT[ID]}`: Message ID if OK is true
* `${BOTSENT[ERROR]}`: Error code if an error occurred
* `${BOTSENT[DESC]}`: Description text for error
* `${BOTSENT[RETRY]}`: Seconds to wait if telegram requests throtteling.
* `$res`: temporary variable containing the full transmission result, may be overwritten by any bashbot function.
By default you don't have to care about retry, as bashbot resend the message after the requested time automatically.
Only if the retry fails also an error is returned. The downside is that send_message functions will wait until resend is done.
If you want to disable automatic error processing and handle all errors manually (or don't care)
set ```BASHBOT_RETRY``` to any no zero value.
set `BASHBOT_RETRY`to any no zero value.
[Telegram Bot API error codes](https://github.com/TelegramBotAPI/errors)
@ -266,9 +265,9 @@ set ```BASHBOT_RETRY``` to any no zero value.
#### Detect bot blocked
If the we can't connect to telegram, e.g. blocked from telegram server but also any other reason,
bashbot set ```BOTSENT[ERROR]``` to ```999```.
bashbot set `BOTSENT[ERROR]`to `999`.
To get a notification on every connection problem create a function named ```bashbotBlockRecover``` and handle blocks there.
To get a notification on every connection problem create a function named `bashbotBlockRecover`and handle blocks there.
If the function returns true (0 or no value) bashbot will retry once and then return to the calling function.
In case you return any non 0 value bashbot will return to the calling function without retry.
@ -302,5 +301,5 @@ Note: If you disable automatic retry, se above, you disable also connection prob
#### [Prev Getting started](2_usage.md)
#### [Next Expert Use](4_expert.md)
#### $$VERSION$$ v1.21-dev-2-gde31231
#### $$VERSION$$ v1.21-dev-29-g13d15f4

View File

@ -5,17 +5,17 @@
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```,
```Hebrew```, ```Arabic``` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more details.
two bytes for encoding and covers almost all `Latin` alphabets, also `Greek`, `Cyrillic`,
`Hebrew`, `Arabic` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more details.
#### Setting up your Environment
In general ```bash``` and ```GNU``` utitities are UTF-8 aware if you to setup your environment
In general `bash` and `GNU` utitities are UTF-8 aware if you to setup your environment
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.
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:
2. Set `Shell` environment to UTF-8 in your `.profile` and your scripts. The usual settings are:
```bash
export 'LC_ALL=C.UTF-8'
@ -35,15 +35,15 @@ export 'LANGUAGE=den_US.UTF-8'
```
3. make sure your bot scripts use the correct settings, eg. include the lines above at the beginning of your scripts
To display all available locales on your system run ```locale -a | more```. [Gentoo Wiki](https://wiki.gentoo.org/wiki/UTF-8)
To display all available locales on your system run `locale -a | more`. [Gentoo Wiki](https://wiki.gentoo.org/wiki/UTF-8)
#### Bashbot UTF-8 Support
Bashbot handles all messages transparently, regardless of the charset in use. The only exception is when converting from JSON data to strings.
Telegram use JSON to send / receive 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.
The Emoticons ``` 😁 😘 ❤️ 😊 👍 ``` are encoded as: ``` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D ```
Telegram use JSON to send / receive 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.
The Emoticons ` 😁 😘 ❤️ 😊 👍 ` are encoded as: ` \uD83D\uDE01 \uD83D\uDE18 \u2764\uFE0F \uD83D\uDE0A \uD83D\uDC4D `
**This "mixed" JSON encoding needs special handling and can not decoded from** ```echo -e``` or ```printf '%s\\n'```
**This "mixed" JSON encoding needs special handling and can not decoded from** `echo -e` or `printf '%s\\n'`
Bbashbot uses an internal, pure bash implementation which is well tested now, even there may some corner cases*.
@ -56,7 +56,7 @@ Setup the environment for the user you want to run bashbot and enter desired use
sudo ./bashbot.sh init
```
Edit the file ```bashbot.rc``` and change the following lines to fit your configuration:
Edit the file `bashbot.rc` and change the following lines to fit your configuration:
```bash
#######################
# Configuration Section
@ -79,7 +79,7 @@ From now on use 'bashbot.rc' to manage your bot:
```bash
sudo ./bashbot.rc start
```
Type ```ps -ef | grep bashbot``` to verify your Bot is running as the desired user.
Type `ps -ef | grep bashbot` to verify your Bot is running as the desired user.
If your Bot is started by 'bashbot.rc', you must use 'bashbot.rc' also to manage your Bot! The following commands are available:
```bash
@ -90,15 +90,15 @@ 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 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 service include a working ```bashbot.rc``` in your init system (systemd, /etc/init.d).
To use bashbot as a system service include a working `bashbot.rc` in your init system (systemd, /etc/init.d).
### Schedule bashbot from Cron
An example crontab is provided in ```examples/bashbot.cron```.
An example crontab is provided in `examples/bashbot.cron`.
- If you are running bashbot with your user-ID, copy the examples lines to your crontab and remove username ```nobody```.
- 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```
- If you are running bashbot with your user-ID, copy the examples lines to your crontab and remove username `nobody`.
- 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`
### Use bashbot from CLI and scripts
@ -209,7 +209,7 @@ If you want to have other locations for config, data etc, define and export the
**Note: all specified directories and files must exist or running 'bashbot.sh' will fail.**
##### BASHBOT_ETC
Location of the files ```commands.sh```, ```mycommands.sh```, ```botconfig.jssh```, ```botacl``` ...
Location of the files `commands.sh`, `mycommands.sh`, `botconfig.jssh`, `botacl` ...
```bash
unset BASHBOT_ETC # keep in telegram-bot-bash (default)
export BASHBOT_ETC "" # keep in telegram-bot-bash
@ -223,7 +223,7 @@ Location of the files ```commands.sh```, ```mycommands.sh```, ```botconfig.jssh`
e.g. /etc/bashbot
##### BASHBOT_VAR
Location of runtime data ```data-bot-bash```, ```count.jssh```
Location of runtime data `data-bot-bash`, `count.jssh`
```bash
unset BASHBOT_VAR # keep in telegram-bot-bash (default)
export BASHBOT_VAR "" # keep in telegram-bot-bash
@ -296,7 +296,7 @@ set BASHBOT_CURL to point to it.
```
##### BASHBOT_WGET
Bashbot uses ```curl``` to communicate with telegram server. if ```curl``` is not available ```wget``` is used.
Bashbot uses `curl` to communicate with telegram server. if `curl` is not available `wget` is used.
If 'BASHBOT_WGET' is set to any value (not undefined or not empty) wget is used even is curl is available.
```bash
unset BASHBOT_WGET # use curl (default)
@ -378,5 +378,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms.
#### [Prev Advanced Use](3_advanced.md)
#### [Next Best Practice](5_practice.md)
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-34-ga5307e3

View File

@ -10,9 +10,9 @@ In addition you should know about [BotFather, the one bot to rule them all](http
If you don't have a github account, it may time to [setup a free account now](https://github.com/pricing)
### Add commands to mycommands.sh only
Do not change ```bashbot.sh``` and ```commands.sh```, instead place your commands in to ```mycommands.sh```.
To start with a clean/minimal bot copy ```mycommands.sh.clean``` to ```mycommands.sh``` and start editing
the message strings and place commands in the```case ... esac``` block of the function mycommands():
Do not change `bashbot.sh` and `commands.sh`, instead place your commands in to `mycommands.sh`.
To start with a clean/minimal bot copy `mycommands.sh.clean` to `mycommands.sh` and start editing
the message strings and place commands in the`case ... esac` block of the function mycommands():
```bash
# file: mycommands.sh
# your additional bashbot commands
@ -39,13 +39,13 @@ mycommands() {
### DIsable, replace and extend global commands
Global bashbot command processing, e.g. /start, /info etc. is disabled if you return a non zero value from ```mycommands.sh```,
Global bashbot command processing, e.g. /start, /info etc. is disabled if you return a non zero value from `mycommands.sh`,
see /start example below.
To replace a global bashbot command add the same command to ```mycommands.sh``` and place ```return 1``` at the end of
To replace a global bashbot command add the same command to `mycommands.sh` and place `return 1` at the end of
the case block, see /kickme example below.
If a command is available as a global command and in ```mycommands.sh```, plus you return a zero value (nothing or 0)
If a command is available as a global command and in `mycommands.sh`, plus you return a zero value (nothing or 0)
both command sections are processed. Thus you can extend global commands with additional actions, see /info example below
**Learn more about [Bot commands](https://core.telegram.org/bots#commands).**
@ -75,7 +75,7 @@ both command sections are processed. Thus you can extend global commands with ad
### Separate logic from commands
If a command need more than 2-3 lines of code, you should use a function to separate logic from command. Place your functions in ```mycommands.sh``` and call the from your command. Example:
If a command need more than 2-3 lines of code, you should use a function to separate logic from command. Place your functions in `mycommands.sh` and call the from your command. Example:
```bash
# file: mycommands.sh
# your additional bashbot commands
@ -133,7 +133,9 @@ Line 17:
^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.
```
As you can see my ```mybotcommands.inc.sh``` contains an useless echo command in 'TEXT=' assignment and can be replaced by ```TEXT="${TEXT}${WORD}"```
As you can see my `mybotcommands.inc.sh` contains an useless echo command in 'TEXT=' assignment and can be replaced by `TEXT="${TEXT}${WORD}"`
```bash
$ shellcheck -x examples/notify
OK
@ -158,5 +160,5 @@ The second warning is about an unused variable, this is true because in our exam
#### [Prev Best Practice](5_practice.md)
#### [Next Functions Reference](6_reference.md)
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-29-g13d15f4

View File

@ -6,11 +6,11 @@
To insert line brakes in a message or caption you can place `\n` in the text.
##### send_action
```send_action``` shows users what your bot is currently doing.
`send_action` shows users what your bot is currently doing.
*usage:* send_action "${CHAT[ID]}" "action"
*"action":* ```typing```, ```upload_photo```, ```record_video```, ```upload_video```, ```record_audio```, ```upload_audio```, ```upload_document```, ```find_location```.
*"action":* `typing`, `upload_photo`, `record_video`, `upload_video`, `record_audio`, `upload_audio`, `upload_document`, `find_location`.
*alias:* _action "action"
@ -21,7 +21,7 @@ send_action "${CHAT[ID]}" "record_audio"
```
##### send_normal_message
```send_normal_message``` sends text only messages to the given chat.
`send_normal_message` sends text only messages to the given chat.
*usage:* send_normal_message "${CHAT[ID]}" "message"
@ -34,7 +34,7 @@ send_normal_message "${CHAT[ID]}" "this is a text message"
##### send_markdownv2_message
```send_markdownv2_message``` sends markdown v2 style messages to the given chat.
`send_markdownv2_message` sends markdown v2 style messages to the given chat.
Telegram supports a new [Markdown V2 Style](https://core.telegram.org/bots/api#markdownv2-style) which
has more formatting codes and is more robust, but incompatible with old telegram markdown style.
@ -48,7 +48,7 @@ send_markdownv2_message "${CHAT[ID]}" "*bold* __underlined__ [text](link)"
##### send_markdown_message
```send_markdown_message``` sends markdown style messages to the given chat.
`send_markdown_message` sends markdown style messages to the given chat.
This is the old, legacy Telegram markdown style, retained for backward compatibility.
It supports a [reduced set of Markdown](https://core.telegram.org/bots/api#markdown-style) only
@ -64,7 +64,7 @@ send_markdown_message "${CHAT[ID]}" "*bold* _italic_ [text](link)"
##### send_html_message
```send_html_message``` sends HTML style messages to the given chat.
`send_html_message` sends HTML style messages to the given chat.
Telegram supports a [reduced set of HTML](https://core.telegram.org/bots/api#html-style) only
*usage:* send_html_message "${CHAT[ID]}" "html message"
@ -78,7 +78,7 @@ send_normal_message "${CHAT[ID]}" "<b>bold</b> <i>italic><i> <em>italic>/em> <a
```
##### forward_message
```forward_mesage``` forwards a message to the given chat.
`forward_mesage` forwards a message to the given chat.
*usage:* forward_message "chat_to" "chat_from" "${MESSAGE[ID]}"
@ -100,7 +100,7 @@ See also [deleteMessage limitations](https://core.telegram.org/bots/api#deleteme
----
##### send_message
```send_message``` sends any type of message to the given chat. Type of output is steered by keywords within the message.
`send_message` sends any type of message to the given chat. Type of output is steered by keywords within the message.
The main use case for send_message is to process the output of interactive chats and background jobs. **For regular Bot commands I recommend using of the dedicated send_xxx_message() functions from above.**
@ -114,7 +114,7 @@ The main use case for send_message is to process the output of interactive chats
##### send_file
send_file allows can send different type's of files, e.g. photos, stickers, audio, media, etc.
send_file can send different type's of files, e.g. photos, stickers, audio, media, etc.
[see Telegram API documentation](https://core.telegram.org/bots/api#sending-files).
It's recommended to use __absolute path names__ (_starting with `/`_), as relative path names are threated as __relative to UPLOADDIR__ `data-bot-bash/upload`!
@ -214,7 +214,7 @@ specify URL buttons, no Text Buttons and the Buttons must be an Array of Buttons
The inline buttons must be specified as a JSON string in the following format:
```[ {"text":"text1", "url":"url1"}, ... {"text":"textN", "url":"urlN"} ]```
`[ {"text":"text1", "url":"url1"}, ... {"text":"textN", "url":"urlN"} ]```
Each button consists of a pair of text and URL values, sourrounded by '{ }', multiple buttons are separated by '**,**' and everything is wrapped in '[ ]'.
@ -245,7 +245,7 @@ To replace a message you must know the message id of the the original message. T
`BOTSENT[ID]` after sending the original message.
##### edit_normal_message
```edit_normal_message``` replace a message with a text message in the given chat.
`edit_normal_message` replace a message with a text message in the given chat.
*usage:* edit_normal_message "${CHAT[ID]}" "MESSAGE-ID" "message"
@ -258,7 +258,7 @@ edit_normal_message "${CHAT[ID]}" "${saved-id}" "this is another text"
```
##### edit_markdownv2_message
```edit_markdownv2_message``` replace a message with a markdown v2 message in the given chat.
`edit_markdownv2_message` replace a message with a markdown v2 message in the given chat.
*usage:* edit_markdownv2_message "${CHAT[ID]}" "MESSAGE-ID" "message"
@ -271,7 +271,7 @@ edit_markdownv2_message "${CHAT[ID]}" "${saved-id}" "this is __markdown__ *V2* t
```
##### edit_markdown_message
```edit_markdown_message``` replace a message with a markdown message in the given chat.
`edit_markdown_message` replace a message with a markdown message in the given chat.
*usage:* edit_markdown_message "${CHAT[ID]}" "MESSAGE-ID" "message"
@ -284,7 +284,7 @@ edit_markdown_message "${CHAT[ID]}" "${saved-id}" "this is *markdown* text"
```
##### edit_html_message
```edit_html_message``` replace a message with a html message in the given chat.
`edit_html_message` replace a message with a html message in the given chat.
*usage:* edit_html_message "${CHAT[ID]}" "MESSAGE-ID" "message"
@ -325,13 +325,13 @@ Your Bot will leave the chat.
*alias:* _leave
```bash
if _is_admin ; then
if bot_is_admin ; then
send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*"
leave_chat "${CHAT[ID]}"
fi
```
'See also [kick Chat Member](https://core.telegram.org/bots/api/#kickchatmember)*
See also [kick Chat Member](https://core.telegram.org/bots/api/#kickchatmember)*
----
@ -475,7 +475,7 @@ Background functions and interactive jobs extends the bot functionality to not o
chats and send messages based on time or other external events.
##### start_proc
```startproc``` starts a script, the output of the script is sent to the user or chat, user input will be sent back to the script. see [Advanced Usage](3_advanced.md#Interactive-Chats)
`startproc` starts a script, the output of the script is sent to the user or chat, user input will be sent back to the script. see [Advanced Usage](3_advanced.md#Interactive-Chats)
*usage:* start_proc "${CHAT[ID]}" "script"
@ -572,38 +572,33 @@ fi
----
##### send_interactive
Form version 0.80 on forward_message is used to forward messages to interactive job. It replaces the old 'inproc' commands used for TMUX.
Usually a message is automatically forwarded in 'commands.sh', but you can forward messages while processing also or send your own messages.
`send_interactive` is used to forward messages to interactive jobs.
Usually a message is automatically forwarded from within `commands.sh`, but you can send messages yourself.
*usage:* send_interactive "${CHAT[ID]}" "message"
*replaces:* incproc
----
### jsshDB
Since output generated by JSON.sh is so handy to use in bash, we use the format for a simple keys/value file store.
The file extensions is '.jssh' and for security reasons location of jssh files is restricted to BASHBOT_ETC and BASHBOT_DATA..
Since output generated by `JSON.sh` is so easy to use in bash, bashbot uses the format for a simple keys/value file store also.
#### fast and slow operations
jsshDB files are simple text files and if you append a new Key/value pairs to the end of the file it overwrites
an existing key/value pair. We use this behavior for "fast" file operations.
jsshDB files are flat text files containing key/value pairs in the `JSON.sh` format.
Key/value pairs appearing later in the file overwrites earlier key/value pairs, Bashbot use this behavior to implement "fast replace" file operations.
"fast functions" add a new key/value pair to the end of the file without deleting an existing one, this is fast but over (long)
time the file grows infinity.
"fast functions" add a new key/value pair to the end of a file without deleting an existing one, this is fast but over time the file grows to infinity.
"slow functions" in contrast modify the key/value pairs in place and write the whole file back,
this is slower but clean up the file. All previously added key/value pairs are replaced
and only the last one is written back to the file.
"slow functions" read the file, modify the key/value pairs in memory and write the whole file back, this is slower but removes duplicate keys from the file.
fast functions:
Fast functions:
```
jssh_insertKeyDB , jssh_addKeyDB , jssh_countKeyDB
```
slow functions:
Slow functions:
```
jssh_writeDB, jssh_updateDB , jssh_deleteKeyDB, jssh_clearDB
@ -612,18 +607,17 @@ slow functions:
#### File naming and locking
A jssh fileDB consists of two files which must reside inside BASHBOT_ETC or BASHBOT_DATA.
A jssh fileDB consists of two files and must reside inside `BASHBOT_ETC` or `BASHBOT_DATA`.
- `filename.jssh` is a text file containing the key/value data in json.sh format.
- `filename.jssh.flock` is used for read/write locking with flock
- `filename.jssh` is the file containing the key/value pairs in JSON.sh format.
- `filename.jssh.flock` is used to provide read/write locking with flock
Path names containing `..` or not located in BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions with an error.
Path names containing `..` or not located in `BASHBOT_ETC` or `BASHBOT_DATA` are refused by jsshDB functions with an error.
Since version 0.94 jsshDB functions support file locking with flock. Write/update operations are serialised with flock to wait until
previous operations are finished, see "man flock" for information. To avoid deadlocks we use a timeout of 10s for write and 5s for read operations.
jsshDB functions use file locking if `flock is available, read/write operations are serialised to wait until
previous operations are finished, see "man flock". To avoid deadlocks bashbot use a timeout of 10s for write and 5s for read operations.
Every jssh_*DB function exist as jssj_*DB_async also.
In case flock is not availibe or you don't want locking, jssh_*DB_async functions without file locking will be used.
For every `jssh_...DB` function a `jsshj_...DB_async` function exists also. In case don't want locking, use `jssh_...DB_async` functions.
*Example:* for allowed file names:
```bash
@ -676,7 +670,7 @@ Something wrong with data-bot-bash/../../../somevalues
```
##### jssh_writeDB
Write content of an ARRAY into jsshDB file. ARRAY name must be declared with "declare -A ARRAY" before calling writeDB.
Write content of an ARRAY into jsshDB file. ARRAY name must be declared with `declare -A ARRAY` before calling writeDB.
"DB" file MUST exist or nothing is written.
Note: Existing content is overwritten.
@ -712,7 +706,7 @@ cat "${DATADIR:-}/myvalues.jssh"
```
##### jssh_printDB
Print content of an ARRAY to STDOUT. ARRAY name must be declared with "declare -A ARRAY" before calling printDB..
Print content of an ARRAY to STDOUT. ARRAY name must be declared with `declare -A ARRAY` before calling printDB..
*usage:* jssh_printDB "ARRAY"
@ -734,7 +728,7 @@ jssh_printDB READVALUES
```
##### jssh_updateDB
Update/Add content of an ARRAY into a jsshDB file. ARRAY name must be declared with "declare -A ARRAY" before calling updateDB.
Update/Add content of an ARRAY into a jsshDB file. ARRAY name must be declared with `declare -A ARRAY` before calling updateDB.
"DB" file MUST exist or nothing is written.
Note: Existing content not in ARRAY is kept in file.
@ -769,7 +763,7 @@ cat "$DBfile"
```
##### jssh_readDB
Read content of a file in JSON.sh format into given ARRAY. ARRAY name must be declared with "declare -A ARRAY" upfront,
Read content of a file in JSON.sh format into given ARRAY. ARRAY name must be declared with `declare -A ARRAY` upfront,
*usage:* jssh_readDB "ARRAY" "filename"
@ -875,8 +869,8 @@ https://linuxconfig.org/how-to-use-arrays-in-bash-script
----
### Aliases - shortcuts for often used functions
Aliases are handy shortcuts for using in 'mycommands.sh', they avoid error prone typing of "${CHAT[ID]}" "${USER[ID]}" as much as possible.
Do not use them in bashbot.sh, modules and addons.
Aliases are handy shortcuts for use in `mycommands.sh` *only*, they avoid error prone typing of "${CHAT[ID]}" "${USER[ID]}" as much as possible.
Do not use them in other files e.g. `bashbot.sh`, modules, addons etc.
##### _is_botadmin
@ -974,7 +968,7 @@ Do not use them in bashbot.sh, modules and addons.
### Helper functions
##### download
Download the fiven URL and returns the final filename in TMPDIR. If the given filename exists,the filename is prefixed with a
Download the given URL and returns the final filename in TMPDIR. If the given filename exists,the filename is prefixed with a
random number. Filename is not allowed to contain '/' or '..'.
*usage:* download URL filename
@ -1084,9 +1078,6 @@ killallproc
##### get_file
*usage:* url="$(get_file "${CHAT[ID]}" "message")"
##### send_text
*usage:* send_text "${CHAT[ID]}" "message"
----
##### JsonDecode
@ -1107,7 +1098,7 @@ Reads JSON from STDIN and Outputs found Value to STDOUT
##### Json2Array
Read JSON.sh style data from STDIN and assign to given ARRAY
ARRAY name must be declared with "declare -A ARRAY" before calling
ARRAY name must be declared with `declare -A ARRAY` before calling
*usage:* Json2Array "ARRAY"
@ -1155,5 +1146,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca
#### [Prev Best Practice](5_practice.md)
#### [Next Notes for Developers](7_develop.md)
#### $$VERSION$$ v1.21-dev-3-g79402ec
#### $$VERSION$$ v1.21-dev-32-g2aa5c75

View File

@ -7,7 +7,7 @@ If you want to provide fixes or new features [fork bashbot on github](https://he
### Debugging Bashbot
Usually all bashbot output is discarded.
If you want to get error messages (and more) start bashbot ```./bashbot.sh startbot debug```.
If you want to get error messages (and more) start bashbot `./bashbot.sh startbot debug`.
you can the change the level of verbosity of the debug argument:
```
@ -29,18 +29,18 @@ Logging of telegram update poll is disabled by default, also in `debug` mode. To
set `BASHBOT_UPDATELOG` to an empty value (not unset) `export BASHBOT_UPDATELOG=""`
### Modules and Addons
**Modules** resides in ```modules/*.sh``` and are colletions of optional bashbot functions grouped by functionality. Main reason for creating modules was
**Modules** resides in `modules/*.sh` and are colletions of optional bashbot functions grouped by functionality. Main reason for creating modules was
to keep 'bashbot.sh' small, while extending functionality. In addition not every function is needed by all bots, so you can
disable modules, e.g. by rename the respective module file to 'module.sh.off'.
Modules must use only functions provided by 'bashbot.sh' or the module itself and should not depend on other modules or addons.
The only mandatory module is 'module/sendMessage.sh'.
If a not mandatory module is used in 'bashbot.sh' or 'commands.sh', the use of ```_is_function``` or
```_execute_if_function``` is mandatory to catch absence of the module.
If a not mandatory module is used in 'bashbot.sh' or 'commands.sh', the use of `_is_function` or
`_execute_if_function` is mandatory to catch absence of the module.
**Addons** resides in ```addons/*.sh.dist``` and are not enabled by default. To activate an addon rename it to end with '.sh', e.g. by
```cp addons/example.sh.dist addons/example.sh```.
**Addons** resides in `addons/*.sh.dist` and are not enabled by default. To activate an addon rename it to end with '.sh', e.g. by
`cp addons/example.sh.dist addons/example.sh`.
Addons must register themself to BASHBOT_EVENTS at startup, e.g. to call a function every time a message is received.
Addons works similar as 'commands.sh' and 'mycommands.sh' but are much more flexible on when functions/commands are triggered.
@ -65,16 +65,16 @@ Note: For the same reason event function MUST return immediately! Time consuming
An RECEIVE event is executed when a Message is received, same iQuery / Message variables are available as in commands.sh
* BASHBOT_EVENT_INLINE an inline query is received
* `BASHBOT_EVENT_INLINE` an inline query is received
* BASHBOT_EVENT_MESSAGE any of the following message types is received
* BASHBOT_EVENT_TEXT a message containing text is received
* BASHBOT_EVENT_CMD a message containing a command is received (starts with /)
* BASHBOT_EVENT_REPLYTO a reply to a message is received
* BASHBOT_EVENT_FORWARD a forwarded message is received
* BASHBOT_EVENT_CONTACT a contact is received
* BASHBOT_EVENT_LOCATION a location or a venue is received
* BASHBOT_EVENT_FILE a file is received
* BASHBOT_EVENT_MESSAGE` any of the following message types is received
* `BASHBOT_EVENT_TEXT` a message containing text is received
* `BASHBOT_EVENT_CMD` a message containing a command is received (starts with /)
* `BASHBOT_EVENT_REPLYTO` a reply to a message is received
* `BASHBOT_EVENT_FORWARD` a forwarded message is received
* `BASHBOT_EVENT_CONTACT` a contact is received
* `BASHBOT_EVENT_LOCATION` a location or a venue is received
* `BASHBOT_EVENT_FILE` a file is received
*usage*: BASHBOT_EVENT_xxx[ "unique-name" ]="callback"
@ -84,6 +84,7 @@ An RECEIVE event is executed when a Message is received, same iQuery / Message v
and "unique-name" is the name provided when registering the event.
*Example:* Register a function to echo to any Text sent to the bot
```bash
# register callback:
BASHBOT_EVENT_TEXT["example_1"]="example_echo"
@ -102,7 +103,7 @@ example_echo() {
An SEND event is executed when a Message is send to telegram.
* BASHBOT_EVENT_SEND is executed if data is send or uploaded to Telegram server
* `BASHBOT_EVENT_SEND` is executed if data is send or uploaded to Telegram server
In contrast to other events, BASHBOT_EVENT_SEND is executed in a sub shell, so there is no need to spawn
a background process for longer running commands and changes to variables are not persistent!
@ -132,13 +133,13 @@ example_log(){
Important: Bashbot timer tick is disabled by default and must be enabled by setting BASHBOT_START_TIMER to any value not zero.
* BASHBOT_EVENT_TIMER executed every minute and can be used in 3 variants: oneshot, once a minute, every X minutes.
* `BASHBOT_EVENT_TIMER` is executed every minute and can be used in 3 variants: oneshot, once a minute, every X minutes.
Registering to BASHBOT_EVENT_TIMER works similar as for message events, but you must add a timing argument to the name.
EVENT_TIMER is triggered every 60s and waits until the current running command is finished, so it's not exactly every
minute, but once a minute.
Every time EVENT_TIMER is triggered the variable "EVENT_TIMER" is increased. each callback is executed if ```EVENT_TIMER % time``` is '0' (true).
Every time EVENT_TIMER is triggered the variable "EVENT_TIMER" is increased. each callback is executed if `EVENT_TIMER % time` is '0' (true).
This means if you register an every 5 minutes callback first execution may < 5 Minutes, all subsequent executions are once every 5. Minute.
*usage:* BASHBOT_EVENT_TIMER[ "name" , "time" ], where time is:
@ -148,7 +149,7 @@ This means if you register an every 5 minutes callback first execution may < 5 M
* x execute every x minutes
* -x execute once WITHIN the next x Minutes (next 10 Minutes since start "event")
Note: If you want exact "in x minutes" use "EVENT_TIMER plus x" as time: ```-(EVENT_TIMER + x)```
Note: If you want exact "in x minutes" use "EVENT_TIMER plus x" as time: `-(EVENT_TIMER + x)`
*Example:*
```bash
@ -185,7 +186,7 @@ Let's create a stripped down version:
- delete not needed commands and functions from `mycommands.sh`
- run `dev/make-standalone.sh` to create a a stripped down version of your bot
Now have a look at the directory 'standalone', here you find the files 'bashbot.sh' and 'commands.sh' containing everything to run your bot.
Now have a look at the directory `standalone`, here you find the files `bashbot.sh` and `commands.sh` containing everything to run your bot.
[Download make-standalone.sh](https://github.com/topkecleon/telegram-bot-bash/blob/master/dev/make-standalone.sh) from github.
### Setup your develop environment
@ -197,16 +198,22 @@ Now have a look at the directory 'standalone', here you find the files 'bashbot.
5. give your (dev) fork a new version tag: `git tag v1.xx`
6. setup github hooks by running `dev/install-hooks.sh`
Note for Debian: Debian Buster has an old shellcheck version, pls update from [buster-backports](https://backports.debian.org/Instructions/)
Run `dev/make-distrubition.sh` to create installation archives and a test installation in `DIST/`.
To update the test installation, e.g. after git pull, local changes or switch master/develop, run `dev/make-distrubition.sh` again.
Note for Debian: Debian Buster ships older versions of many utilities, pls try to install from [buster-backports](https://backports.debian.org/Instructions/)
```bash
sudo apt-get -t buster-backports install git shellcheck pandoc codespell curl
```
#### Test, Add, Push changes
A typical bashbot develop loop looks as follow:
1. start developing - *change, copy, edit bashbot files ...*
2. after changing a bash sript: ```shellcheck -x script.sh```
3. ```dev/all-tests.sh``` - *in case if errors back to 2.*
4. ```dev/git-add.sh``` - *check for changed files, update version string, run git add*
5. ```git commit -m "COMMIT MESSAGE"; git push```
2. after changing a bash sript: `shellcheck -x script.sh`
3. `dev/all-tests.sh` - *in case if errors back to 2.*
4. `dev/git-add.sh` - *check for changed files, update version string, run git add*
5. `git commit -m "COMMIT MESSAGE"; git push`
**If you setup your dev environment with hooks and use the scripts above, versioning, adding and testing is done automatically.**
@ -260,23 +267,23 @@ For more examples see [Pure bash bible](https://github.com/dylanaraps/pure-bash-
#### Prepare a new version
After some development it may time to create a new version for the users. a new version can be in sub version upgrade, e.g. for fixes and smaller additions or
a new release version for new features. To mark a new version use ```git tag NEWVERSION``` and run ```dev/version.sh``` to update all version strings.
a new release version for new features. To mark a new version use `git tag NEWVERSION` and run `dev/version.sh` to update all version strings.
Usually I start with pre versions and when everything looks good I push out a release candidate (rc) and finally the new version.
```
v0.x-devx -> v0.x-prex -> v0.x-rc -> v0.x ... 0.x+1-dev ...
```
If you release a new Version run ```dev/make-distribution.sh``` to create the zip and tar.gz archives in the dist directory and attach them to the github release. Do not forget to delete directory dist afterwards.
If you release a new Version run `dev/make-distribution.sh` to create the zip and tar.gz archives in the dist directory and attach them to the github release. Do not forget to delete directory dist afterwards.
#### Versioning
Bashbot is tagged with version numbers. If you start a new development cycle you can tag your fork with a version higher than the current version.
E.g. if you fork 'v0.60' the next develop version should tagged as ```git tag "v0.61-dev"``` for fixes or ```git tag "v0.70-dev"``` for new features.
E.g. if you fork 'v0.60' the next develop version should tagged as `git tag "v0.61-dev"` for fixes or `git tag "v0.70-dev"` for new features.
To get the current version name of your develepment fork run ```git describe --tags```. The output looks like ```v0.70-dev-6-g3fb7796``` where your version tag is followed by the number of commits since you tag your branch and followed by the latest commit hash. see also [comments in version.sh](../dev/version.sh)
To get the current version name of your develepment fork run `git describe --tags`. The output looks like `v0.70-dev-6-g3fb7796` where your version tag is followed by the number of commits since you tag your branch and followed by the latest commit hash. see also [comments in version.sh](../dev/version.sh)
To update the Version Number in files run ```dev/version.sh files```, it will update the line '#### $$VERSION$$ ###' in all files to the current version name.
To update the Version Number in files run `dev/version.sh files`, it will update the line '#### $$VERSION$$ ###' in all files to the current version name.
To update version in all files run 'dev/version.sh' without parameter.
@ -284,27 +291,27 @@ To update version in all files run 'dev/version.sh' without parameter.
For a shell script running as a service it's important to be paranoid about quoting, globbing and other common problems. So it's a must to run shellchek on all shell scripts before you commit a change. this is automated by a git hook activated in Setup step 6.
To run shellcheck for a single script run ```shellcheck -x script.sh```, to check all schripts run ```dev/hooks/pre-commit.sh```.
To run shellcheck for a single script run `shellcheck -x script.sh`, to check all schripts run `dev/hooks/pre-commit.sh`.
### bashbot test suite
Starting with version 0.70 bashbot has a test suite. To start testsuite run ```dev/all-tests.sh```. all-tests.sh will return 'SUCCESS' only if all tests pass.
Starting with version 0.70 bashbot has a test suite. To start testsuite run `dev/all-tests.sh`. all-tests.sh will return 'SUCCESS' only if all tests pass.
#### enabling / disabling tests
All tests are placed in the directory ```test```. To disable a test remove the execute flag from the '*-test.sh' script, to (re)enable a test make the script executable again.
All tests are placed in the directory `test`. To disable a test remove the execute flag from the '*-test.sh' script, to (re)enable a test make the script executable again.
#### creating new tests
To create a new test run ```test/ADD-test-new.sh``` and answer the questions, it will create the usually needed files and dirs:
To create a new test run `test/ADD-test-new.sh` and answer the questions, it will create the usually needed files and dirs:
Each test consists of a script script named after ```p-name-test.sh``` *(where p is test pass 'a-z' and name the name
of your test)* and an optional dir ```p-name-test/``` *(script name minus '.sh')* for additional files.
Each test consists of a script script named after `p-name-test.sh` *(where p is test pass 'a-z' and name the name
of your test)* and an optional dir `p-name-test/` *(script name minus '.sh')* for additional files.
Tests with no dependency to other tests will run in pass 'a', tests which need an initialized bashbot environment must run in pass 'd' or later.
A temporary test environment is created when 'ALL-tests.sh' starts and deleted after all tests are finished.
The file ```ALL-tests.inc.sh``` must be included from all tests and provide the test environment as shell variables:
The file `ALL-tests.inc.sh` must be included from all tests and provide the test environment as shell variables:
```bash
# Test Environment
TESTME="$(basename "$0")"
@ -351,5 +358,5 @@ fi
#### [Prev Function Reference](6_reference.md)
#### $$VERSION$$ v1.20-3-g232a16b
#### $$VERSION$$ v1.21-dev-40-g889fe8e

View File

@ -5,8 +5,8 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
# shellcheck disable=SC1117
#### $$VERSION$$ v1.20-0-g2ab00a2
# shellcheck disable=SC1117,SC2059
#### $$VERSION$$ v1.21-dev-1-g03bdecb
# will be automatically sourced from bashbot
@ -129,7 +129,7 @@ job_control() {
ADM="$(getConfigKey "botadmin")"
debug_checks "Enter job_control" "${1}"
for FILE in "${DATADIR:-.}/"*-back.cmd; do
[ "${FILE}" = "${DATADIR:-.}/*-back.cmd" ] && echo -e "${RED}No background processes.${NC}" && break
[ "${FILE}" = "${DATADIR:-.}/*-back.cmd" ] && printf "${RED}No background processes.${NN}" && break
content="$(< "${FILE}")"
CHAT="${content%%:*}"
job="${content#*:}"

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-33-gd083390
#
# source from commands.sh to use jsonDB functions
#
@ -331,8 +331,9 @@ function jssh_updateArray_async() {
# read JSON.sh style data and asssign to an ARRAY
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
Json2Array() {
# match ["....."]\t and replace \t with = and print quote true false escape not escaped $
# shellcheck disable=SC1091,SC1090
[ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" )
[ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/p' -e 's/=(true|false)/="\1"/' -e 's/([^\]|^)\$/\1\\$/g')" )
}
# get Config Key from jssh file without jsshDB
# output ARRAY as JSON.sh style data

View File

@ -6,7 +6,7 @@
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
# shellcheck disable=SC1117
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-29-g13d15f4
# will be automatically sourced from bashbot
@ -155,14 +155,6 @@ send_button() {
}
UPLOADDIR="${BASHBOT_UPLOAD:-${DATADIR}/upload}"
# for now this can only send local files with curl!
# extend to allow send files by URL or telegram ID
send_file() {
upload_file "${@}"
}
if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then
# there are no checks if URL or ID exists
# $1 chat $3 ... $n URL or ID
@ -189,17 +181,41 @@ else
}
fi
UPLOADDIR="${BASHBOT_UPLOAD:-${DATADIR}/upload}"
# for now this can only send local files with curl!
# extend to allow send files by URL or telegram ID
send_file() {
local err
upload_file "${@}"; err="$?"
# fake Telegram response to provide error
if [ "${err}" != "0" ]; then
BOTSENT=()
BOTSENT[OK]="false"
case "$err" in
1) BOTSENT[ERROR]="Path to file $2 contains to much '../' or starts with '.'";;
2) BOTSENT[ERROR]="Path to file $2 does not match regex: ${FILE_REGEX} ";;
3) if [[ "$2" == "/"* ]];then
BOTSENT[ERROR]="File not found: $2"
else
BOTSENT[ERROR]="File not found: ${UPLOADDIR}/$2"
fi;;
esac
[ -n "${BASHBOTDEBUG}" ] && log_message "Error in upload_file: ${BOTSENT[ERROR]}"
fi
}
upload_file(){
local CUR_URL WHAT STATUS text=$3 file="$2"
# file access checks ...
[[ "$file" = *'..'* ]] && return # no directory traversal
[[ "$file" = '.'* ]] && return # no hidden or relative files
[[ "$file" = *'..'* ]] && return 1 # no directory traversal
[[ "$file" = '.'* ]] && return 1 # no hidden or relative files
if [[ "$file" = '/'* ]] ; then
[[ ! "$file" =~ $FILE_REGEX ]] && return # absolute must match REGEX
[[ ! "$file" =~ ${FILE_REGEX} ]] && return 2 # absolute must match REGEX
else
file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # othiers must be in UPLOADDIR
fi
[ ! -r "$file" ] && return # and file must exits of course
[ ! -r "$file" ] && return 3 # and file must exits of course
local ext="${file##*.}"
case $ext in
@ -286,7 +302,7 @@ send_message() {
keyboard="$(sed <<< "${2}" '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere *//;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"
btext="$(sed <<< "${2}" '/mybtextstartshere /!d;s/.*mybtextstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"
burl="$(sed <<< "${2}" '/myburlstartshere /!d;s/.*myburlstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//g;s/ *mykeyboardendshere.*//g')"
file="$(sed <<< "${2}" '/myfilelocationstartshere /!d;s/.*myfilelocationstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"
file="$(sed <<< "${2}" '/myfile[^s]*startshere /!d;s/.*myfile[^s]*startshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"
lat="$(sed <<< "${2}" '/mylatstartshere /!d;s/.*mylatstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"
long="$(sed <<< "${2}" '/mylongstartshere /!d;s/.*mylongstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"
title="$(sed <<< "${2}" '/mytitlestartshere /!d;s/.*mytitlestartshere //;s/ *my[kfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"
@ -308,7 +324,7 @@ send_message() {
sent=y
fi
if [ -n "$file" ]; then
upload_file "$1" "$file" "$text"
send_file "$1" "$file" "$text"
sent=y
fi
if [ -n "$lat" ] && [ -n "$long" ]; then
@ -320,14 +336,14 @@ send_message() {
sent=y
fi
if [ "$sent" != "y" ];then
send_text "$1" "$text"
send_text_mode "$1" "$text"
fi
}
# $1 CHAT $2 message starting possibly with html_parse_mode or markdown_parse_mode
# not working, fix or remove after 1.0!!
send_text() {
send_text_mode() {
case "$2" in
'html_parse_mode'*)
send_html_message "$1" "${2//html_parse_mode}"

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-45-gddb2785
# common variables
export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME
@ -33,8 +33,13 @@ export INPUTFILE REFFILE INPUTFILE2 REFFILE2 OUTPUTFILE
INPUTFILE2="${DIRME}/${REFDIR}/${REFDIR}2.input"
REFFILE2="${DIRME}/${REFDIR}/${REFDIR}2.result"
# reset ENVIRONMENT
export BASHBOT_URL TESTTOKEN BOTTOKEN BASHBOT_HOME BASHBOT_VAR BASHBOT_ETC
BOTTOKEN=""
BASHBOT_HOME=""
BASHBOT_VAR=""
BASHBOT_ETC=""
# do not query telegram when testing
export BASHBOT_URL TESTTOKEN
BASHBOT_URL="https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?"
TESTTOKEN="123456789:BASHBOTTESTSCRIPTbashbottestscript_"

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-26-gbe87ba3
# include common functions and definitions
# shellcheck source=test/ALL-tests.inc.sh

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-22-ga3efcd2
# include common functions and definitions
# shellcheck source=test/ALL-tests.inc.sh
@ -45,10 +45,10 @@ touch "$ALLOW/this_is_my.gif" "$ALLOW/this_is_my.doc"
touch "$DATADIR/this_is_my.gif" "$DATADIR/this_is_my.doc"
while read -r line ; do
echo -n "."
set -x
set -x; set +e
send_message "123456" "$line" >>"${OUTPUTFILE}"
set +x
set +x; set -e
echo -n "."
done < "${INPUTFILE}" 2>>"${LOGFILE}"
[ -d "$ALLOW" ] && rm -rf "$ALLOW"

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
#### $$VERSION$$ v1.20-0-g2ab00a2
#### $$VERSION$$ v1.21-dev-10-g9bfc27a
# include common functions and definitions
# shellcheck source=test/ALL-tests.inc.sh
@ -31,7 +31,7 @@ set +f
# shellcheck disable=SC2086
cp ${TESTDIR}/*commands.sh "${BASHBOT_ETC}" || exit 1
set -f
cp -r "${TESTDIR}/bashbot.sh" "${TESTDIR}/modules" "${BASHBOT_BIN}" || exit 1
cp -r "${TESTDIR}/bashbot.sh" "${TESTDIR}/modules" "${TESTDIR}/JSON.sh/JSON.sh" "${BASHBOT_BIN}" || exit 1
TESTFILES="${TOKENFILE} ${ACLFILE}"