|
|
|
|
|
|
|
import re
|
|
|
|
import telebot
|
|
|
|
from config import Config
|
|
|
|
from ai_assistant import AI_assistant
|
|
|
|
import pdfplumber
|
|
|
|
import time
|
|
|
|
# 使用Token來初始化一個telebot.TeleBot物件,並將其儲存到bot這個變數中
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
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
|
|
|
|
startime = time.time()
|
|
|
|
url = "https://www.bea.gov/news/2024/personal-income-and-outlays-december-2023"
|
|
|
|
text = find_indicate(url)
|
|
|
|
return text
|
|
|
|
def find_indicate_bs4(url):
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
import requests
|
|
|
|
response = requests.get(url)
|
|
|
|
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
|
|
|
|
# 使用CSS選擇器找到元素
|
|
|
|
element = soup.select("tr",class_ ='item-fact-row' )
|
|
|
|
|
|
|
|
return element[1].text.split('+')[0]+" "+element[1].text.split('+')[1]
|
|
|
|
def find_indicate(url):
|
|
|
|
from selenium import webdriver
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.chrome.options import Options
|
|
|
|
import time
|
|
|
|
options = Options()
|
|
|
|
# options.add_argument("--headless") # 啟用無頭模式
|
|
|
|
driver =webdriver.Chrome(options=options)
|
|
|
|
driver.get(url)
|
|
|
|
# time.sleep(3)
|
|
|
|
chat = driver.find_element(By.XPATH, '//*[@id="home"]/div[2]/div/div/div[1]/table/tbody/tr[13]/td[6]')
|
|
|
|
return chat.text
|
|
|
|
|
|
|
|
# 是一個持續運行的迴圈,不斷從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")
|
|
|
|
page = pdf.pages[0]
|
|
|
|
text = page.extract_text().split('\n')
|
|
|
|
text = (text[7]+text[8]).split(',')
|
|
|
|
text = text[0]+text[1]+text[2]
|
|
|
|
return text
|
|
|
|
def read_nonfarm():
|
|
|
|
startimee = time.time()
|
|
|
|
for i in range(7,13):
|
|
|
|
print(f"2022年{i}月非農就業人數: ", end= "" )
|
|
|
|
print(read_pdf_nonfarm(i, 23))
|
|
|
|
|
|
|
|
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)
|
|
|
|
startimee = time.time()
|
|
|
|
print(find_indicate_bs4("https://www.bea.gov/data/personal-consumption-expenditures-price-index"))
|
|
|
|
endtimee = time.time()
|
|
|
|
print("Time_NonFarm: ", endtimee-startimee)
|
|
|
|
# cfg = Config()
|
|
|
|
# tg_bot(cfg)
|