Skip to content

Instantly share code, notes, and snippets.

@j40903272
Last active May 27, 2022 08:00
Show Gist options
  • Save j40903272/42de97943bc0540a1deaaad36ce5ca5e to your computer and use it in GitHub Desktop.
Save j40903272/42de97943bc0540a1deaaad36ce5ca5e to your computer and use it in GitHub Desktop.
telegram exmaple
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