122
TelegramBot.py
Normal file
122
TelegramBot.py
Normal file
@@ -0,0 +1,122 @@
|
||||
from datetime import datetime
|
||||
import telegram
|
||||
import asyncio
|
||||
import platform
|
||||
from multiprocessing import Pool
|
||||
import os
|
||||
|
||||
class TelegramBot:
|
||||
enable = None
|
||||
BOT_TOKEN = None
|
||||
CHANNEL_ID = None
|
||||
client = None
|
||||
|
||||
def __init__(self, enable=True):
|
||||
"""
|
||||
텔레그램 봇 설정
|
||||
|
||||
환경변수 설정 방법:
|
||||
export TELEGRAM_BOT_TOKEN="your_bot_token_here"
|
||||
export TELEGRAM_CHAT_ID="your_chat_id_here"
|
||||
|
||||
또는 .env 파일 생성:
|
||||
TELEGRAM_BOT_TOKEN=your_bot_token_here
|
||||
TELEGRAM_CHAT_ID=your_chat_id_here
|
||||
|
||||
botname: coinbot
|
||||
username for the bot: ncue_coin_bot
|
||||
token to access the HTTP API: 6435061393:AAHOh9wB5yGNGUdb3SfCYJrrWTBe7wgConM
|
||||
|
||||
botname: lottobot
|
||||
username for the bot: ncue_lotto_bot
|
||||
token to access the HTTP API:6791293398:AAFi1zrQTs6UmuHycAuNdsBgHDHaHcOJcYA
|
||||
|
||||
botname: stockbot
|
||||
username for the bot: ncue_stock_bot
|
||||
token to access the HTTP API: 6874078562:AAEHxGDavfc0ssAXPQIaW8JGYmTR7LNUJOw
|
||||
"""
|
||||
|
||||
|
||||
# 환경변수에서 토큰과 채팅 ID 읽기
|
||||
self.token = os.getenv('TELEGRAM_BOT_TOKEN', '6791293398:AAFi1zrQTs6UmuHycAuNdsBgHDHaHcOJcYA')
|
||||
self.chat_id = os.getenv('TELEGRAM_CHAT_ID', '574661323')
|
||||
|
||||
self.botname = "lottobot"
|
||||
self.username = "ncue_lotto_bot"
|
||||
self.client = telegram.Bot(token=self.token)
|
||||
self.enable = enable
|
||||
|
||||
if self.token.startswith('6791293398'):
|
||||
print("⚠️ 경고: 기본 토큰을 사용 중입니다. 보안을 위해 환경변수 설정을 권장합니다.")
|
||||
|
||||
return
|
||||
|
||||
|
||||
# https://velog.io/@gyunghoe/%ED%85%94%EB%A0%88%EA%B7%B8%EB%9E%A8-%EB%B4%87-%EC%84%B1%EB%8A%A5-%EC%B5%9C%EC%A0%81%ED%99%94%ED%95%98%EA%B8%B0
|
||||
|
||||
@staticmethod
|
||||
def send(text):
|
||||
token = os.getenv('TELEGRAM_BOT_TOKEN', '6791293398:AAFi1zrQTs6UmuHycAuNdsBgHDHaHcOJcYA')
|
||||
chat_id = os.getenv('TELEGRAM_CHAT_ID', '574661323')
|
||||
|
||||
client = telegram.Bot(token=token)
|
||||
if platform.system().lower() == 'windows':
|
||||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
||||
asyncio.run(client.send_message(chat_id=chat_id, text=text))
|
||||
return
|
||||
|
||||
def alarm_live(self, stock_code, stock_name):
|
||||
if self.enable:
|
||||
this_time = datetime.now()
|
||||
text = "[ALIVE] {} {} ({})".format(this_time.strftime('%H:%M'), stock_code, stock_name)
|
||||
pool = Pool(12)
|
||||
pool.map(self.send, [text])
|
||||
print(" * "+text)
|
||||
|
||||
return
|
||||
|
||||
def post(self, stock_code, stock_name, type, price, count, rsi, balance=0):
|
||||
if self.enable:
|
||||
this_time = datetime.now()
|
||||
if 0 < balance:
|
||||
text = "{}, {}, code: {}, name: {}, amount: {}, price: {}, count: {}, (balance: {:.2f}), (rsi: {:.2f})".format(type, this_time.strftime('%H:%M'), stock_code, stock_name, price*count, price, count, balance, rsi)
|
||||
else:
|
||||
text = "{}, {}, code: {}, name: {}, amount: {}, price: {}, count: {}, (rsi: {:.2f})".format(type, this_time.strftime('%H:%M'), stock_code, stock_name, price*count, price, count, rsi)
|
||||
pool = Pool(12)
|
||||
pool.map(self.send, [text])
|
||||
print(" * "+text)
|
||||
return
|
||||
|
||||
def sendMsg(self, msg):
|
||||
if self.enable:
|
||||
this_time = datetime.now()
|
||||
text = "[{}]: \n{}".format(this_time.strftime("%Y-%m-%d %H:%M:%S"), msg)
|
||||
pool = Pool(12)
|
||||
pool.map(self.send, [text])
|
||||
print(" * "+text)
|
||||
|
||||
return
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
telegramBot = TelegramBot()
|
||||
|
||||
"""
|
||||
last_weekend = '20240727'
|
||||
p_no = 1130
|
||||
p_ball = [15, 19, 21, 25, 27, 27]
|
||||
ymd = '20240803'
|
||||
howto = 1
|
||||
random_state = 28
|
||||
cluster = 31
|
||||
recommend_size = 0
|
||||
|
||||
p_str = "[지난주] {}\n - {} 회차, {}\n[금주] {}\n - {} 회차\n[모델 #6_c{}_{}_{}] ({}개)\n".format(
|
||||
last_weekend, p_no, str(p_ball), ymd, (p_no + 1), howto, random_state, cluster, recommend_size
|
||||
)
|
||||
telegramBot.sendMsg("{}".format(p_str))
|
||||
"""
|
||||
|
||||
telegramBot.sendMsg("🎯 DeepLottery 시스템 테스트 메시지")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user