telegram-bot-bash/index.html
2016-10-25 00:19:54 +02:00

365 lines
15 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Telegram-bot-bash by danog</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="stylesheets/normalize.css" media="screen">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen">
<link rel="stylesheet" type="text/css" href="stylesheets/github-light.css" media="screen">
</head>
<body>
<section class="page-header">
<h1 class="project-name">Telegram-bot-bash</h1>
<h2 class="project-tagline">Telegram bot written in bash</h2>
<a href="https://github.com/danog/telegram-bot-bash" class="btn">View on GitHub</a>
<a href="https://github.com/danog/telegram-bot-bash/zipball/master" class="btn">Download .zip</a>
<a href="https://github.com/danog/telegram-bot-bash/tarball/master" class="btn">Download .tar.gz</a>
</section>
<section class="main-content">
<h1>
<a id="bashbot" class="anchor" href="#bashbot" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>bashbot</h1>
<p>A Telegram bot written in bash.</p>
<p>Depends on <a href="http://github.com/tmux/tmux">tmux</a>.
Uses <a href="http://github.com/dominictarr/JSON.sh">JSON.sh</a>.</p>
<p>Released to the public domain wherever applicable.
Elsewhere, consider it released under the <a href="http://www.wtfpl.net/txt/copying/">WTFPLv2</a>.</p>
<h2>
<a id="instructions" class="anchor" href="#instructions" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Instructions</h2>
<h3>
<a id="create-your-first-bot" class="anchor" href="#create-your-first-bot" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Create your first bot</h3>
<ol>
<li>
<p>Message <a href="https://github.com/botfather" class="user-mention">@botfather</a> <a href="https://telegram.me/botfather">https://telegram.me/botfather</a> with the following
text: <code>/newbot</code>
If you don't know how to message by username, click the search
field on your Telegram app and type <code>@botfather</code>, you should be able
to initiate a conversation. Be careful not to send it to the wrong
contact, because some users has similar usernames to <code>botfather</code>.</p>
<p><img src="http://i.imgur.com/aI26ixR.png" alt="botfather initial conversation"></p>
</li>
<li><p><a href="https://github.com/botfather" class="user-mention">@botfather</a> replies with <code>Alright, a new bot. How are we going to
call it? Please choose a name for your bot.</code></p></li>
<li><p>Type whatever name you want for your bot.</p></li>
<li><p><a href="https://github.com/botfather" class="user-mention">@botfather</a> replies with <code>Good. Now let's choose a username for your
bot. It must end in bot. Like this, for example: TetrisBot or
tetris_bot.</code></p></li>
<li><p>Type whatever username you want for your bot, minimum 5 characters,
and must end with <code>bot</code>. For example: <code>telesample_bot</code></p></li>
<li>
<p><a href="https://github.com/botfather" class="user-mention">@botfather</a> replies with:</p>
<p>Done! Congratulations on your new bot. You will find it at
telegram.me/telesample_bot. You can now add a description, about
section and profile picture for your bot, see /help for a list of
commands.</p>
<p>Use this token to access the HTTP API:
<b>123456789:AAG90e14-0f8-40183D-18491dDE</b></p>
<p>For a description of the Bot API, see this page:
<a href="https://core.telegram.org/bots/api">https://core.telegram.org/bots/api</a></p>
</li>
<li><p>Note down the 'token' mentioned above.</p></li>
<li>
<p>Type <code>/setprivacy</code> to <a href="https://github.com/botfather" class="user-mention">@botfather</a>.</p>
<p><img src="http://i.imgur.com/tWDVvh4.png" alt="botfather later conversation"></p>
</li>
<li><p><a href="https://github.com/botfather" class="user-mention">@botfather</a> replies with <code>Choose a bot to change group messages settings.</code></p></li>
<li><p>Type <code>@telesample_bot</code> (change to the username you set at step 5
above, but start it with <code>@</code>)</p></li>
<li>
<p><a href="https://github.com/botfather" class="user-mention">@botfather</a> replies with</p>
<p>'Enable' - your bot will only receive messages that either start
with the '/' symbol or mention the bot by username.
'Disable' - your bot will receive all messages that people send to groups.
Current status is: ENABLED</p>
</li>
<li><p>Type <code>Disable</code> to let your bot receive all messages sent to a
group. This step is up to you actually.</p></li>
<li><p><a href="https://github.com/botfather" class="user-mention">@botfather</a> replies with <code>Success! The new status is: DISABLED. /help</code></p></li>
</ol>
<h3>
<a id="install-bashbot" class="anchor" href="#install-bashbot" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Install bashbot</h3>
<p>Clone the repository:</p>
<pre><code>git clone --recursive https://github.com/topkecleon/telegram-bot-bash
</code></pre>
<p>Create a file called token and paste the token in there.
Then start editing the commands.</p>
<h3>
<a id="receive-data" class="anchor" href="#receive-data" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Receive data</h3>
<p>You can read incoming data using the following variables:</p>
<ul>
<li>
<code>$MESSAGE</code>: Incoming messages</li>
<li>
<code>$MESSAGE[ID]</code>: ID of incoming message</li>
<li>
<code>$CAPTION</code>: Captions</li>
<li>
<code>$USER</code>: This array contains the First name, last name, username and user id of the sender of the current message.
<ul>
<li>
<code>${USER[ID]}</code>: User id</li>
<li>
<code>${USER[FIRST_NAME]}</code>: User's first name</li>
<li>
<code>${USER[LAST_NAME]}</code>: User's last name</li>
<li>
<code>${USER[USERNAME]}</code>: Username</li>
</ul>
</li>
<li>
<code>$CHAT</code>: This array contains the First name, last name, username, title and user id of the chat of the current message.
<ul>
<li>
<code>${CHAT[ID]}</code>: Chat id</li>
<li>
<code>${CHAT[FIRST_NAME]}</code>: Chat's first name</li>
<li>
<code>${CHAT[LAST_NAME]}</code>: Chat's last name</li>
<li>
<code>${CHAT[USERNAME]}</code>: Username</li>
<li>
<code>${CHAT[TITLE]}</code>: Title</li>
<li>
<code>${CHAT[TYPE]}</code>: Type</li>
<li>
<code>${CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]}</code>: All members are administrators (true if true)</li>
</ul>
</li>
<li>
<code>$URLS</code>: This array contains documents, audio files, stickers, voice recordings and stickers stored in the form of URLs.
<ul>
<li>
<code>${URLS[AUDIO]}</code>: Audio files</li>
<li>
<code>${URLS[VIDEO]}</code>: Videos</li>
<li>
<code>${URLS[PHOTO]}</code>: Photos (maximum quality)</li>
<li>
<code>${URLS[VOICE]}</code>: Voice recordings</li>
<li>
<code>${URLS[STICKER]}</code>: Stickers</li>
<li>
<code>${URLS[DOCUMENT]}</code>: Any other file</li>
</ul>
</li>
<li>
<code>$CONTACT</code>: This array contains info about contacts sent in a chat.
<ul>
<li>
<code>${CONTACT[NUMBER]}</code>: Phone number</li>
<li>
<code>${CONTACT[FIRST_NAME]}</code>: First name</li>
<li>
<code>${CONTACT[LAST_NAME]}</code>: Last name</li>
<li>
<code>${CONTACT[ID]}</code>: User id</li>
</ul>
</li>
<li>
<code>$LOCATION</code>: This array contains info about locations sent in a chat.
<ul>
<li>
<code>${LOCATION[LONGITUDE]}</code>: Longitude</li>
<li>
<code>${LOCATION[LATITUDE]}</code>: Latitude</li>
</ul>
</li>
</ul>
<h3>
<a id="usage" class="anchor" href="#usage" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Usage</h3>
<p>To send messages use the <code>send_message</code> function:</p>
<pre><code>send_message "${CHAT[ID]}" "lol"
</code></pre>
<p>To send html or markdown put the following strings before the text, depending on the parsing mode you want to enable:</p>
<pre><code>send_message "${CHAT[ID]}" "markdown_parse_mode lol *bold*"
</code></pre>
<pre><code>send_message "${CHAT[ID]}" "html_parse_mode lol &lt;b&gt;bold&lt;/b&gt;"
</code></pre>
<p>This function also allows a third parameter that disables additional function parsing (for safety use this when reprinting user input):</p>
<pre><code>send_message "${CHAT[ID]}" "lol" "safe"
</code></pre>
<p>To send images, videos, voice files, photos ecc use the <code>send_photo</code> function (remember to change the safety Regex @ line 14 of command.sh to allow sending files only from certain directories):</p>
<pre><code>send_file "${CHAT[ID]}" "/home/user/doge.jpg" "Lool"
</code></pre>
<p>To send custom keyboards use the <code>send_keyboard</code> function:</p>
<pre><code>send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" "Yep" "No"
</code></pre>
<p>To send locations use the <code>send_location</code> function:</p>
<pre><code>send_location "${CHAT[ID]}" "Latitude" "Longitude"
</code></pre>
<p>To send venues use the <code>send_venue</code> function:</p>
<pre><code>send_venue "${CHAT[ID]}" "Latitude" "Longitude" "Title" "Address" "optional foursquare id"
</code></pre>
<p>To forward messages use the <code>forward</code> function:</p>
<pre><code>forward "${CHAT[ID]}" "from_chat_id" "message_id"
</code></pre>
<p>To send a chat action use the <code>send_action</code> 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.</p>
<pre><code>send_action "${CHAT[ID]}" "action"
</code></pre>
<p>To create interactive chats, write (or edit the question script) a normal bash (or C or python) script, chmod +x it and then change the argument of the startproc function to match the command you usually use to start the script.
The text that the script will output will be sent in real time to the user, and all user input will be sent to the script (as long as it's running or until the user kills it with /cancel).
To open up a keyboard in an interactive script, print out the keyboard layout in the following way:</p>
<pre><code>echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\""
</code></pre>
<p>Same goes for files:</p>
<pre><code>echo "Text that will appear in chat? myfilelocationstartshere /home/user/doge.jpg"
</code></pre>
<p>And locations:</p>
<pre><code>echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45"
</code></pre>
<p>And venues:</p>
<pre><code>echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45 mytitlestartshere my home myaddressstartshere Diagon Alley N. 37"
</code></pre>
<p>You can combine them:</p>
<pre><code>echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\" myfilelocationstartshere /home/user/doge.jpg mylatstartshere 45 mylongstartshere 45"
</code></pre>
<p>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.</p>
<p>The following commands allows users to interact with your bot via <em>inline queries</em>.
In order to enable <strong>inline mode</strong>, send <code>/setinline</code> command to <a href="https://telegram.me/botfather">@BotFather</a> and provide the placeholder text that the user will see in the input field after typing your bots name.
Also, edit line 12 from <code>commands.sh</code> putting a "1".
Note that you can't modify the first two parameters of the function <code>answer_inline_query</code>, only the ones after them.</p>
<p>To send messsages or links through an <em>inline query</em>:</p>
<pre><code>answer_inline_query "$iQUERY_ID" "article" "Title of the result" "Content of the message to be sent"
</code></pre>
<p>To send photos in jpeg format and less than 5MB, from a website through an <em>inline query</em>:</p>
<pre><code>answer_inline_query "$iQUERY_ID" "photo" "A valid URL of the photo" "URL of the thumbnail"
</code></pre>
<p>To send standard gifs from a website (less than 1MB) through an <em>inline query</em>:</p>
<pre><code>answer_inline_query "$iQUERY_ID" "gif" "gif url"
</code></pre>
<p>To send mpeg4 gifs from a website (less than 1MB) through an <em>inline query</em>:</p>
<pre><code>answer_inline_query "$iQUERY_ID" "mpeg4_gif" "mpeg4 gif url"
</code></pre>
<p>To send videos from a website through an <em>inline query</em>:</p>
<pre><code>answer_inline_query "$iQUERY_ID" "video" "valid video url" "Select one mime type: text/html or video/mp4" "URL of the thumbnail" "Title for the result"
</code></pre>
<p>To send photos stored in Telegram servers through an <em>inline query</em>:</p>
<pre><code>answer_inline_query "$iQUERY_ID" "cached_photo" "identifier for the photo"
</code></pre>
<p>To send gifs stored in Telegram servers through an <em>inline query</em>:</p>
<pre><code>answer_inline_query "$iQUERY_ID" "cached_gif" "identifier for the gif"
</code></pre>
<p>To send mpeg4 gifs stored in Telegram servers through an <em>inline query</em>:</p>
<pre><code>answer_inline_query "$iQUERY_ID" "cached_mpeg4_gif" "identifier for the gif"
</code></pre>
<p>To send stickers through an <em>inline query</em>:</p>
<pre><code>answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker"
</code></pre>
<p>To modify the responses to commands edit the commands.sh file (this should ease upgrades of the bot core).</p>
<p>Once you're done editing start the bot with <code>./bashbot.sh start</code>. If you want to do some more changes make them and then rerun the same command.
To stop the bot run <code>./bashbot.sh kill</code>.
If some thing doesn't work as it should, debug with <code>bash -x bashbot.sh</code>.</p>
<p>To use the functions provided in this script in other scripts simply source bashbot: <code>source bashbot.sh</code></p>
<h2>
<a id="user-count" class="anchor" href="#user-count" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>User count</h2>
<p>To count the total number of users that ever used the bot run the following command:</p>
<pre><code>bash bashbot.sh count
</code></pre>
<h2>
<a id="sending-broadcasts-to-all-users" class="anchor" href="#sending-broadcasts-to-all-users" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Sending broadcasts to all users</h2>
<p>To send a broadcast to all of users that ever used the bot run the following command:</p>
<pre><code>bash bashbot.sh broadcast "Hey! I just wanted to let you know that the bot's been updated!"
</code></pre>
<p>That's it!</p>
<p>If you feel that there's something missing or if you found a bug, feel free to submit a pull request!</p>
<footer class="site-footer">
<span class="site-footer-owner"><a href="https://github.com/danog/telegram-bot-bash">Telegram-bot-bash</a> is maintained by <a href="https://github.com/danog">danog</a>.</span>
<span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a> using the <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a> by <a href="https://twitter.com/jasonlong">Jason Long</a>.</span>
</footer>
</section>
</body>
</html>