|
|
|
@ -1,84 +1,23 @@ |
|
|
|
|
|
|
|
|
|
import re |
|
|
|
|
import telebot |
|
|
|
|
from config import Config |
|
|
|
|
from ai_assistant import AI_assistant |
|
|
|
|
import pdfplumber |
|
|
|
|
import time |
|
|
|
|
# 使用Token來初始化一個telebot.TeleBot物件,並將其儲存到bot這個變數中 |
|
|
|
|
import requests |
|
|
|
|
from datetime import datetime |
|
|
|
|
import schedule |
|
|
|
|
|
|
|
|
|
BOT_TOKEN = '6701395239:AAFE30dqvNihDdni9vYoAbWssO-X5yAmwho' |
|
|
|
|
# BOT_TOKEN = "6746720034:AAEMaoV2FwIZ8pz_PF18-bo2a6gFC1eVtVs" |
|
|
|
|
#BOT_TOKEN = '6589162555:AAHGhrTQ0wYNtIUySMohnfpxQl1d6blr24Q' |
|
|
|
|
bot = telebot.TeleBot(BOT_TOKEN) |
|
|
|
|
|
|
|
|
|
user_url = None |
|
|
|
|
user_answer = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 定義消息處理器,當收到網址時 |
|
|
|
|
# 考慮到medium也有提供短網址,所以暫不把qffers判斷寫進去 |
|
|
|
|
@bot.message_handler(func=lambda msg: re.search(r'http[s]?://(www\.)?(link\.)?medium\.com/', msg.text) if msg.text else False) |
|
|
|
|
def handle_medium_url(message): |
|
|
|
|
# bot.reply_to(message, "想了解什麼訊息呢?問問我吧!") # 這邊改到上面不確定會不會比較好 |
|
|
|
|
|
|
|
|
|
global user_url, identify |
|
|
|
|
user_url = message.text |
|
|
|
|
#contents, lang, identify = get_contents(user_url) |
|
|
|
|
#save_to_storage(contents, identify) |
|
|
|
|
bot.reply_to(message, "想了解什麼訊息呢?問問我吧!") |
|
|
|
|
|
|
|
|
|
@bot.message_handler(func=lambda msg: re.search(r'http[s]?://', msg.text) if msg.text else False) |
|
|
|
|
def handle_other_url(message): |
|
|
|
|
bot.reply_to(message, "此網頁不支援唷😂😂\n請試試看輸入 https://link.medium.com/rxe98Z708Db ", disable_web_page_preview=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 定義消息處理器,當用戶發送一個消息時,機器人將回覆相同的消息 |
|
|
|
|
@bot.message_handler(func=lambda msg: msg.text.lower() in ["hi", "hello", "嗨", "你好", "早上好", "晚上好", "早安", "晚安", "介紹", "誰"]) |
|
|
|
|
def reply_all(message): |
|
|
|
|
print(message) #你可以出來看看會有啥 |
|
|
|
|
print(message.text) #單純擷取文字 |
|
|
|
|
user_first_name = message.from_user.first_name |
|
|
|
|
intro = f'嗨, {user_first_name}!👋👋\n我們是睿富者(QFFERS)\n歡迎到我們的Medium文章看看~ https://link.medium.com/rxe98Z708Db \n \n也歡迎試著貼上Medium文章網址問個問題吧!😂😂\n' |
|
|
|
|
result = start() |
|
|
|
|
print(result) |
|
|
|
|
bot.reply_to(message, result, disable_web_page_preview=True) |
|
|
|
|
|
|
|
|
|
@bot.message_handler(func=lambda msg: True) |
|
|
|
|
def handle_user_answer(message): |
|
|
|
|
global user_answer , startime |
|
|
|
|
user_answer = message.text |
|
|
|
|
# result = answering(user_answer) |
|
|
|
|
|
|
|
|
|
result = start() |
|
|
|
|
print("This is result", result) |
|
|
|
|
end =time.time() |
|
|
|
|
print("Time: ", end-startime) |
|
|
|
|
bot.reply_to(message, result) |
|
|
|
|
|
|
|
|
|
# Using assistant API to answer question |
|
|
|
|
def answering(query): |
|
|
|
|
files = ai_assistant.get_files() |
|
|
|
|
ai_assistant.create_assistant( |
|
|
|
|
name="QFFERS Bot", |
|
|
|
|
instructions="你是一個天問Bot機器人,你的任務是請基於用戶上傳的PDF上找尋用戶所要找尋的答案、數值。" |
|
|
|
|
"任務說明:用戶提問時,請仔細分析問題並提供基於上傳PDF。如果答案來自PDF檔案請提供該篇PDF的段落,若沒有資料請回答:我不知道", |
|
|
|
|
tools=[{"type": "retrieval"}], |
|
|
|
|
files=files |
|
|
|
|
) |
|
|
|
|
ai_assistant.create_thread() |
|
|
|
|
ai_assistant.add_message_to_thread( |
|
|
|
|
role="user", |
|
|
|
|
content=query |
|
|
|
|
def broadcast_message(message:str,chat_id:str): |
|
|
|
|
r = requests.post(f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage", |
|
|
|
|
json={ |
|
|
|
|
"chat_id": chat_id, |
|
|
|
|
"text": message, |
|
|
|
|
}, |
|
|
|
|
) |
|
|
|
|
ai_assistant.run_assistant( |
|
|
|
|
instructions="Please user's language to answer the question. You can only answer according to the uploaded files.") |
|
|
|
|
ai_assistant.check_run(thread_id=ai_assistant.thread.id, run_id=ai_assistant.run.id) |
|
|
|
|
total_price , content = ai_assistant.process_messages() |
|
|
|
|
return content |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start(): |
|
|
|
|
global startime |
|
|
|
@ -112,13 +51,6 @@ def find_indicate(url): |
|
|
|
|
# 是一個持續運行的迴圈,不斷從Telegram伺服器抓取新的消息 |
|
|
|
|
# 然後使用上面定義的消息處理器來處理這些消息。 |
|
|
|
|
|
|
|
|
|
def tg_bot(cfg: Config): |
|
|
|
|
"""Run the Telegram bot.""" |
|
|
|
|
global ai_assistant, storage ,bot |
|
|
|
|
print("Starting Telegram bot...") |
|
|
|
|
# ai_assistant = AI_assistant(cfg) |
|
|
|
|
# 啟動Telegram Bot |
|
|
|
|
bot.infinity_polling() |
|
|
|
|
#非農就業人數 |
|
|
|
|
def read_pdf_nonfarm(month, year): |
|
|
|
|
pdf = pdfplumber.open(f"empsit/empsit_{month}_{year}.pdf") |
|
|
|
@ -136,16 +68,38 @@ def read_nonfarm(): |
|
|
|
|
endtimee = time.time() |
|
|
|
|
print("Time: ", endtimee-startimee) |
|
|
|
|
# print(text.split('\n')[7:9]) |
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
#非農 |
|
|
|
|
startimee = time.time() |
|
|
|
|
print(f"2023年7月非農就業人數: ", end= "" ) |
|
|
|
|
print(read_pdf_nonfarm(7, 23)) |
|
|
|
|
endtimee = time.time() |
|
|
|
|
print("Time_NonFarm: ", endtimee-startimee) |
|
|
|
|
|
|
|
|
|
def read_PCE(): |
|
|
|
|
message = find_indicate_bs4("https://www.bea.gov/data/personal-consumption-expenditures-price-index") |
|
|
|
|
|
|
|
|
|
return message |
|
|
|
|
# def read_PCE_test(): |
|
|
|
|
# from bs4 import BeautifulSoup |
|
|
|
|
# import requests |
|
|
|
|
# response = requests.get("http://127.0.0.1:5000") |
|
|
|
|
# soup = BeautifulSoup(response.text, 'html.parser') |
|
|
|
|
# |
|
|
|
|
# # 使用CSS選擇器找到元素 |
|
|
|
|
# element = soup.select("p") |
|
|
|
|
# print(element[0].text) |
|
|
|
|
# return element[0].text |
|
|
|
|
def broadcast_all(target:str): |
|
|
|
|
startimee = time.time() |
|
|
|
|
print(find_indicate_bs4("https://www.bea.gov/data/personal-consumption-expenditures-price-index")) |
|
|
|
|
message = read_PCE() |
|
|
|
|
while message.split(' ')[0] != target: |
|
|
|
|
message = read_PCE() |
|
|
|
|
broadcast_message(message, "-1002033782195") |
|
|
|
|
endtimee = time.time() |
|
|
|
|
print("Time_NonFarm: ", endtimee-startimee) |
|
|
|
|
# cfg = Config() |
|
|
|
|
# tg_bot(cfg) |
|
|
|
|
broadcast_message( |
|
|
|
|
f"Now :{datetime.fromtimestamp(time.time())} , Spend time :{str(round(endtimee - startimee, 3))} s", |
|
|
|
|
"-1002033782195") |
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
#PCE |
|
|
|
|
print("Start Time:" , datetime.fromtimestamp(time.time())) |
|
|
|
|
schedule.every().day.at("20:05").do(broadcast_all) |
|
|
|
|
while True: |
|
|
|
|
if datetime.now().strftime("%Y-%m-%d %H:%M") == "2024-02-29 20:05": |
|
|
|
|
schedule.run_pending() |
|
|
|
|
time.sleep(0.1) # Check every 0.1 seconds |
|
|
|
|
broadcast_all("December") |
|
|
|
|