Skip to content

Commit

Permalink
Telegram notify support added
Browse files Browse the repository at this point in the history
  • Loading branch information
farzadex-eth committed May 22, 2023
1 parent 4d0a957 commit e7e081d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
18 changes: 18 additions & 0 deletions examples/sample_telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from tokenfinderbot.tokenfinderbot import TokenBot

# bot instance
bot = TokenBot()

# get bot settings
settings = bot.get_settings()

# change bot telegram settings
settings.telegram.notify = True
settings.telegram.bot_token = "YOUR TELEGRAM BOT TOKEN" # your telegram bot token
settings.telegram.chat_id = "xxxxxxxxxx" # chat id for chat, group or channel which the bot is a memeber of

# set new settings
bot.set_settings(settings)

# run with new settings
bot.run()
36 changes: 33 additions & 3 deletions tokenfinderbot/tokenfinderbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ def __init__(self) -> None:
'min_liq': 1000,
'min_mc': 100000
},
'telegram': {
'notify': False,
'bot_token': "",
'chat_id': ""
},
'update_interval': 15,
'db_name': 'db'
}
self._settings = DotMap(default_settings)
self._telegram_bot_token = ""
self._telegram_chat_id = 0

def get_settings(self) -> DotMap:
"""Returns current bot settings
Expand Down Expand Up @@ -71,16 +78,18 @@ def single_run(self) -> None:

# filter pools by liquidity and market cap
filtered_pairs = filter_pools_by_liq_mc(pairs,
mc_liq_ratio=self._settings.liq_mc_filter.mc_liq_ratio, min_mc=self._settings.liq_mc_filter.min_mc,
liq_mc_ratio=self._settings.liq_mc_filter.mc_liq_ratio, min_mc=self._settings.liq_mc_filter.min_mc,
min_liq=self._settings.liq_mc_filter.min_liq)
print("*** Filtered pools by liquidity and market cap")

# write filtered pools to database and print in terminal
# write filtered pools to database and print in terminal and send on telegram
new_pairs_num = 0
for pair in filtered_pairs:
try:
pooldb.insert_pool(pair)
print(pooldb.get_pool_str(pair['pairAddress']))
pool_message = pooldb.get_pool_str(pair['pairAddress'])
print(pool_message)
self.notify_on_telegram(pool_message)
new_pairs_num += 1
except:
pass
Expand All @@ -91,6 +100,26 @@ def single_run(self) -> None:

print("************************************************")


def notify_on_telegram(self, message) -> None:
"""Notify new pools on telegram
Args:
message (str): message
Raises:
BaseException: Error in telegram communication
"""
tsettings = self._settings.telegram
print(tsettings)
if tsettings.notify and tsettings.bot_token != "" and tsettings.chat_id != "":
try:
url = f'https://api.telegram.org/bot{tsettings.bot_token}/sendMessage?chat_id={tsettings.chat_id}&text={message}'
response = requests.post(url)
except Exception as e:
raise BaseException(f"Telegram Error: {e}")


def run(self) -> None:
"""Runs the main function every x minutes
"""
Expand All @@ -105,5 +134,6 @@ def main_job():
# run the task every x minutes
while True:
run_pending()



0 comments on commit e7e081d

Please sign in to comment.