Last active
May 27, 2022 08:00
-
-
Save j40903272/42de97943bc0540a1deaaad36ce5ca5e to your computer and use it in GitHub Desktop.
telegram exmaple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
import telegram | |
from telethon import TelegramClient, sync | |
============================================================================================================ | |
msg = { | |
"symbol": "BTC", | |
"action": "BUY", | |
# below optional | |
# "entry": 20000, | |
# "stop_loss": 19000, | |
# "take_profit": 30000, | |
} | |
msg = json.dumps(msg) | |
============================================================================================================ | |
# telethon (send message as your account, need to create api_id at https://my.telegram.org/auth, close 2fa) | |
def send_by_telethon(msg: str): | |
session_name = 'CTA' | |
telegram_api_id = config["telegram_api_id"] | |
telegram_api_hash = config["telegram_api_hash"] | |
telegram_client = TelegramClient(session_name, telegram_api_id, telegram_api_hash) | |
def get_IDs(): | |
# iterate all dialogs to load entities in cache | |
for dialog in telegram_client.iter_dialogs(): | |
# print(dialog.name, dialog.id) | |
pass | |
def send(msg: str, channel: str): | |
telegram_client.start() | |
get_IDs() | |
channel = telegram_client.get_entity(channel) | |
telegram_client.send_message(channel, msg) | |
channel_name = "CTA Strategy" # or channel link | |
send(msg, channel_name) | |
============================================================================================================ | |
# python-telegram-bot (send message as bot, need to setup a bot with telegram 'bot father') | |
chat_id = "channel id" | |
BOT_TOKEN = "Your bot token" | |
bot = telegram.Bot(token=BOT_TOKEN) | |
bot.send_message(text=msg, chat_id=chat_id) | |
============================================================================================================ | |
# webhook | |
access_token = "I will have to create one" | |
webhook_url = "I will have to create one" | |
headers = { | |
'Content-Type': 'application/json', | |
"Authorization": f"Bearer {access_token}" | |
} | |
response = requests.post( | |
url=url, | |
json=msg, | |
headers=headers | |
) | |
assert response.status_code == 200 | |
print(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment