前面有談到爬蟲網站抓情資,Telegram上面也有許多情資,有個網站提供許多相關的頻道,來試試看能不能爬。https://github.com/fastfire/deepdarkCTI/blob/main/telegram.md
官方說有兩種方式
We offer two kinds of APIs for developers. The Bot API allows you to easily create programs that use Telegram messages for an interface. The Telegram API and TDLib allow you to build your own customized Telegram clients.
查看邦友文章,照著建立自己的Bot
https://ithelp.ithome.com.tw/articles/10262881
跟bot的談話訊息可以用下面API取得內容
https://api.telegram.org/********:***************/getUpdates
{
"ok": true,
"result": [
{
"update_id": *******,
"message": {
"message_id": 3,
"from": {
"id": *******,,
"is_bot": false,
"first_name": "test",
"last_name": "123",
"username": "test123",
"language_code": "zh-hans"
},
"chat": {
"id": *******,,
"first_name": "test",
"last_name": "123",
"username": "test123",
"type": "private"
},
"date": 1695541505,
"text": "Hello World!"
}
}
]
}
建了一個頻道測試,當沒有把Bot加入頻道就沒辦法取得訊息,但需求是加入頻道收集資訊,這方法看起來不可行。
取得API ID
https://core.telegram.org/api/obtaining_api_id
使用telethon
https://docs.telethon.dev/en/stable/
!pip3 install telethon
!pip install nest_asyncio
import asyncio
from telethon.sync import TelegramClient
api_id = 'YOUR_API_ID'
api_hash = 'YOUR_API_HASH'
phone_number = 'YOUR_PHONE_NUMBER'
async def read_chat_history():
async with TelegramClient(phone_number, api_id, api_hash) as client:
chat_id = 't.me/xxxx' # 替換為您要讀取的聊天的ID
messages = await client.get_messages(chat_id, limit=10) # 讀取最新10條消息
for message in messages:
print(f"{message.sender_id}: {message.text}")
if __name__ == "__main__":
asyncio.run(read_chat_history())