Skip to content

Instantly share code, notes, and snippets.

@gsam1

gsam1/article.md Secret

Created September 10, 2022 11:29
Show Gist options
  • Save gsam1/97368e8fc9f170eaea3aa31bbe3f7e0b to your computer and use it in GitHub Desktop.
Save gsam1/97368e8fc9f170eaea3aa31bbe3f7e0b to your computer and use it in GitHub Desktop.

Simple Telegram Screener

In our current times staying on top of the latest signals can be challenging. Especially if the screeners are not automated to provide the most up-to-date information on the latest signals. That's why tying a screener to telegram can make one aware of a lot of opportunities. It can notify you when a new signal is found (and we all get the dopamine hit when a new notification arrives on our smartphones). You can also share this screener with people for fun (and of course profit).

To be able to follow along you

  • know you way around Python
  • have a Telegram account
  • have a screener, that outputs a string when it finds a new signal

Working with telegram

Although there are many excellent solutions to sending your signals over to telegram I preffer to use pyTelegramBotAPI - you just need a token and you are off to the races.

import telebot

bot = telebot.TeleBot("TOKEN", parse_mode=None)

How to get your telegram token

In order to get your telegram token you need to go through a couple of steps:

  1. Open Telegram and find @botfarther
  2. [Optional] To see all available commands type /help
  3. To create a new bot just write /newbot
  4. Go through the prompts from botfather
  5. At the end it will give you your API key.

How to get your channel id

  1. Login with a browser version of telegram: https://web.telegram.org/
  2. Click on your channel.
  3. At the end of your current url after the # should be your channel id. It always starts with a minus.

Combining it with your screener:

import telebot


def run_screener() -> str:
    '''
        This is where you would develop your screener.
        It is important to note - you should return a string.
    '''
    return "I generated a new signal!"

TOKEN = "<YOUR-TOKEN-HERE>"
CHANNEL = "<YOUR-CHANNEL-ID-HERE>"
tb = telebot.TeleBot(TOKEN, parse_mode=None)

msg = run_screener()

tb.send_message(CHANNEL, msg)

However do note that this bot is only sending messages to a telegram channel and does not respond to any messages, targeted at it. But for our use case, this is plenty.

Running the screener on a schedule

As shown above, to send a single message its quite simple. A way to automate it is to host that script and run it on a schedule using crontab - good article on it.

Another way to automate it, within python is to use a libary to automate this process. I used schedule.

Something to take into an account is that you need to wrap all of your code into a single function. A simple refactor you could do is shown below.

import telebot


TOKEN = "<YOUR-TOKEN-HERE>"
CHANNEL = "<YOUR-CHANNEL-ID-HERE>"

tb = telebot.TeleBot(TOKEN, parse_mode=None)

def run_screener() -> str:
    '''
        This is where you would develop your screener.
        It is important to note - you should return a string.
    '''
    return "I generated a new signal!"

def send_telegram_signals() -> None:
    msg = run_screener()
    tb.send_message(CHANNEL, msg)

Adding schedule

Lets assume you want to send a message every hour, exactly at 00.

import schedule

schedule.every().hour.at("00").do(send_telegram_signals)

Easy enough.

Combinding it all together

import telebot
import schedule



TOKEN = "<YOUR-TOKEN-HERE>"
CHANNEL = "<YOUR-CHANNEL-ID-HERE>"

tb = telebot.TeleBot(TOKEN, parse_mode=None)

def run_screener() -> str:
    '''
        This is where you would develop your screener.
        It is important to note - you should return a string.
    '''
    return "I generated a new signal!"

def send_telegram_signals() -> None:
    msg = run_screener()
    tb.send_message(CHANNEL, msg)


if __name__ == '__main__':
    schedule.every().hour.at("00").do(send_telegram_signals)

Conclusion

In this article I showcased how easy it is to send your screener notifications and signals over to telegram and run it on a schedule. That's all there is to it.

If you have any questions, suggestions or thoughts, always feel free to share them with me! And if you like to see more how-tos on deploying your screeners and strategies, make sure to follow me and receive the most recent updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment