Code Monkey home page Code Monkey logo

telegram.sh's Introduction

telegram.sh

What does it do?

Telegram offers the feature of bots. A bot allows automated systems and servers to send telegram messages to users. Quite often it can be useful to send stuff to yourself. A classic application of this would be receiving results of cronjob tasks via email. Or maybe you want to grab a small file from your server, but downloading it via SCP would be too much work or wouldn't work at all because firewall stuff / filters / proxy servers / whatever.

telegram.sh allows you to send such things via telegram.

Examples

# Send a message to yourself, using a bot token and a chat_id.
telegram -t 123456:AbcDefGhi-JklMnoPrw -c 12345 "Hello, World."

# You can define the token and chat_id in environment variables or config files.
# Then you can just use
telegram "Hello, World."

# Split them into multiple lines
telegram "Hello,"$'\n'"World."
# or
echo -e "Hello\nWorld." | telegram -

# Or you send this one message to another chat:
telegram -c 6789 "Hello, Mars."

# You can also send messages to multiple chats:
telegram -c 1234 -c 6789 "Hello, Planets."

# Send stuff via stdin. It will automatically be sent as monospace code:
ls -l | telegram -

# Use markdown in your message (HTML is available as well):
telegram -M "To *boldly* go, where _no man_ has gone before."

# Send a local file.
telegram -f results.txt "Here are the results."

# Or an image, giving you a preview and stuff.
telegram -i solar_system.png # We don't need to send a message if we're
# sending a file.

# Or a video.
telegram -V video.mp4

# Use environment variables to tell curl to use a proxy server:
HTTPS_PROXY="socks5://127.0.0.1:1234" telegram "Hello, World."
# Check the curl documentation for more info about supported proxy
# protocols.

Requirements

Only bash and curl. Listing known chats with -l requires jq, but you can easily use this tool without this.

Installation / configuration

  • Grab the latest telegram file from this repository and put it somewhere.
  • Create a bot at telegram:
    • Search for the user @botfather at telegram and start a chat with him.
    • Use the /newbot command to create a new bot. BotFather will give you a token. Keep this.
  • Use your telegram client to send a message to your new bot. Any message will do.
  • Find your chat id. Run telegram.sh with -l: telegram -t <TOKEN> -l. If you have jq installed, it will nicely list its known chats. The number at the front is your chat id. If you don't have jq installed, it will print a bit of JSON data and tell you what to look for.
  • You now have your token and your chat id. Send yourself a first message: telegram -t <TOKEN> -c <CHAT ID> "Hello there."

Carrying the token and the chat id around can be quite cumbersome. You can define them in 6 different ways:

  1. In a file /etc/telegram.sh.conf.
  2. In a file ~/.telegram.sh.
  3. In a file ~/.telegram.sh.conf.
  4. In a file in the same folder as this script, called ./telegram.sh.conf.
  5. In environment variables TELEGRAM_TOKEN and TELEGRAM_CHAT.
  6. As seen above as parameters.

Later variants overwrite earlier variants, so you could define token and chat in /etc/telegram.sh.conf and then overwrite the token with your own in ~/.telegram.sh or on the command line.

The files should look like this:

TELEGRAM_TOKEN="123456:AbcDefGhi-JlkMno"
TELEGRAM_CHAT="12345678"

Multiple chat ids can be defined in a config file as a bash array:

TELEGRAM_TOKEN="123456:AbcDefGhi-JlkMno"
CHATS=(12345678, 23456789, 34567)

With such config command telegram.sh "Hello world" will deliver message to all listed chat ids.

Please be aware that you should keep your token a secret.

You can also add permanent proxy settings in there by adding:

export HTTPS_PROXY="socks5://127.0.0.1:1234"

See the curl documentation for more information about which proxy protocols are supported.

Docker image

docker build -t telegram:latest .
docker run -it --rm telegram

Changelog

Version 0.5

  • New option -Vto send a video file.
  • Configuration can now also be set from a file called .telegram.sh.conf in the same folder as this script.

Version 0.4

  • New option -m to receive the last received message. You could use this e.g. to regularly poll the last message and react on commands. Format of the response is <Message ID> <Sender ID> <Chat ID> <Text>. You could use this feature like this:
    telegram -m | read message_id sender_id chat_id text
    echo "MessageID: $message_id"
    echo "Text:      $text"
    

Contributors

  • abadroot
  • dbarthe
  • hugows
  • kgizdov
  • KOPACb
  • rerime
  • rusalex
  • sergiks

telegram.sh's People

Contributors

abadroot avatar dbarthe avatar fabianonline avatar kgizdov avatar kopacb avatar rerime avatar rusalex avatar sergiks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

telegram.sh's Issues

Uploading error

I'm using your script to send files to a channel but seems it can't upload files larger than 50mb.
I'm getting this error

Telegram reported an error:
 </html>nter>nginx/1.12.2</center>o Large</h1></center>d>
Quitting.

Can't send an image

This is what I get:

$ ./telegram -i '/home/aviram/Media/Photos/Festas e Pessoas/2019-99 Camera Roll/2019.01.26-16.42.01 • Clara na bicicleta 【iPhone XS】.jpg'
curl reported an error. Exit code was: 26.
Response was:
Quitting.

or even:

$ ./telegram -f ~/Media/Photos/Festas\ e\ Pessoas/2019-99\ Camera\ Roll/2019.01.26-16.42.01\ •\ Clara\ na\ bicicleta\ 【iPhone\ XS】.jpg
curl reported an error. Exit code was: 26.
Response was:
Quitting.

[Enhancement] Add "Edit Sent Message" Functionality

Please add the functionality to edit last sent message in the specified chat/group/channel.

This is useful when posting from a continuous work which needs to output status of it's situation from time to time, but multiple messages tend to annoy users in a group/channel.

proxy settings

Hi. Useful option is a proxy. In some countries telegram blocked.

Feature Request

Dear Developer, Dear Sirs,
first of all I would like to thank you for this extremely useful bash script, it is working fine !
My question / features request is to expand it in order to get also messages from telegram, is is possible?
I already searched the GitHub and I found telegram-cli but it's (or seems to ) abandoned.
Please consider this request or if it's impossible, introduce me any Linux (better Ubuntu 22.04 based) that can receive from telegram.
Thanks and Best Regards,
Mike Kranidis

make it installable

Make this script installable via curl or wget

please look at how rustup and nvm (and many others) have done it:

  • rustup curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • nvm curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

How to send a file with spaces or/and brackets

I've got a problem with sending a file which contains spaces and/or brackets.

I want to send "/tmp/(2019) Test/test.jpg" with telegram.sh -i /tmp/(2019) Test/test.jpg, but the answer is -bash: syntax error near unexpected token ('`

If I try telegram.sh /tmp/test 1.jpg the answer is The file /tmp/test does not exist.

Is there a solution?

Conf file in the same folder as the script doesn't seem to work

Hi!
first of all, thanks for this great script. With the latest version, I created a file telegram.sh.conf with:

TELEGRAM_TOKEN="1999969932:..."
TELEGRAM_CHAT="599931726"

I put this file in the same folder as the script, even cded to that folder (is it necessary?) but:

[mano@aralex AUR]$ ./telegram.sh ciao
No bot token was given.

If I move the conf file to the home dir, renaming it with a leading dot, it all works. Is this correct? From what I read on the README, the first method should work too.

Thanks a lot,

  G.

newline \n?

How can I send the new line (\n) in the message?

URL snapshot support

What would you say about adding a simple function:

Command:
telegram <url_img_or_video> "msg"

How to use in public scripts ?

Hi,

I want to use this bot helper in public scripts for logging in Telegram.
But I could not imagine how do I protect my bot token from theft, cause it needs to be set in command line.

Does exist "user" tokens for just sending messages without admin rights ?

Thanks a lot. Cheers.

Please create a tag/release - for use with Homebrew

Nothing fancy needed, just something like:

  1. git tag v0.0.4
  2. git push --tags
  3. gh release create --draft --title "$(shell git describe --tags)"

Without a tag/release not sure if I (or any third-party) can make a Homebrew formula.

Homebrew makes installing so much easier!

Send image with message

What is the correct way to send an image along with a message? In my bash script 'bash telegram.sh - i image.png "message" only shows the message but no image.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.