核心需求:用python 登录steam帐号然后给好友发送消息,以及 接受消息。
普通用户steam web chat地址:https://steamcommunity.com/chat/
可能有用的API接口:https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logon/v1
steam API文档地址:https://partner.steamgames.com/doc%EF%BC%8Chttps://steamapi.xpaw.me/
可能有用的python库:steampy.chat中有 send_message 方法, 但是我不会用,试了2天了,response.code_state = 400
已知用的是web socket协议通讯的。
chatgpt给的代码也是各种BUG,无法运行。因为steamchat有过一次改版。老版本的代码和API接口都被弃用了。
可以使用SteamKit2库。SteamKit2是一个开源的python库,可以帮助您实现steam消息收发。您可以在GitHub上搜索SteamKit2,下载并安装它,然后使用它的API来实现steam消息收发。
该回答引用ChatGPT
有问题可以回复我
from steampy.client import SteamClient
from steampy.utils import get_web_session
from steampy.chat import SteamChat
# 登录steam账号
steam_client = SteamClient()
steam_client.login(username='your_username', password='your_password')
# 获取一个requests.Session对象
web_session = get_web_session()
# 创建 SteamChat 实例
steam_chat = SteamChat(steam_client, web_session)
# 获取好友列表
friends = steam_chat.get_friends()
# 向第一个好友发送消息
friend = friends[0]
steam_chat.send_message(friend.get('steamid'), 'Hello, World!')
# 接收新消息
while True:
new_messages = steam_chat.fetch_messages()
for message in new_messages:
print(f"New message from {message['sender']}:\n{message['message']}")
先安装库
pip install steampy
然后你在你的steam账号设置中启用两步验证。启用后,获取一个私人密钥,该密钥用于通过API进行身份验证。也可以通过访问此网址 https://steamcommunity.com/dev/apikey 来获取API密钥。
进行登录和发送消息:
from steampy.client import SteamClient
from steampy.chat import SteamChat
from getpass import getpass
# 输入您的Steam帐号和密码
username = input("请输入Steam账号:")
password = getpass("请输入Steam密码:")
# 输入您的API密钥
api_key = input("请输入API密钥:")
# 创建一个SteamClient对象
client = SteamClient(api_key)
# 登录您的Steam账号
client.login(username, password)
# 创建一个SteamChat对象
steam_chat = SteamChat(client)
# 获取您的好友列表
friends = steam_chat.get_friends()
# 打印好友列表
print("您的好友列表:")
for friend in friends:
print(friend.get_name())
# 输入您要发送消息的好友名称
friend_name = input("请输入好友名称:")
# 获取好友对象
friend = steam_chat.get_friend_by_name(friend_name)
# 输入您要发送的消息
message = input("请输入您要发送的消息:")
# 发送消息给好友
steam_chat.send_message(friend.steam_id, message)
# 接收消息
while True:
messages = steam_chat.get_messages()
for message in messages:
print(f"{message.author_name}: {message.text}")
要用Python实现Steam消息收发,您可以使用steampy库,这是一个Python库,可帮助您与Steam API进行交互。
首先,您需要安装steampy库,可以使用pip进行安装:
pip install steampy
然后,您需要从Steam获取自己的API密钥。 您可以通过以下步骤进行获取:
登录 Steam 帐户。
访问 Steamworks 开发者网站(https://partner.steamgames.com%29./
单击“我的账户”。
在页面底部找到“Web API密钥”,然后单击“生成新密钥”。
有了API密钥,您现在可以使用以下Python代码登录并发送消息给好友:
from steampy.client import SteamClient
from steampy.login import LoginCredentials
from steampy.exceptions import InvalidCredentials, SteamGuardRequired, CaptchaRequired
from steampy.chat import SteamChat
from getpass import getpass
# 输入账号信息
username = input('请输入Steam帐号:')
password = getpass('请输入Steam密码:')
# 创建 SteamClient 对象
client = SteamClient()
client.login(LoginCredentials(username, password))
# 登录 Steam Chat
chat = SteamChat(client)
# 获取在线好友列表
online_friends = chat.fetch_friends()
# 向在线好友发送消息
if online_friends:
friend_id = online_friends[0]['steamid']
message = '你好,这是通过Python发送的消息!'
chat.send_message(friend_id, message)
# 退出登录
client.logout()
该代码首先使用您提供的用户名和密码登录到Steam帐户,然后使用SteamChat类向在线好友发送消息。 请注意,如果您启用了 Steam Guard 或 Captcha,您可能需要额外的步骤来验证您的帐户。
要接收消息,您可以使用SteamClient对象的get_web_session方法获取requests.Session对象,并使用它来访问https://steamcommunity.com/chat%E3%80%82 在页面中,您可以使用BeautifulSoup或其他类似的库来提取最新的消息并作出响应。
答案来自gpt,参考:
安装 steampy 库:可以使用 pip install steampy 命令来安装该库。
获取 API 令牌:要访问 Steam API,您需要使用 API 令牌。您可以在 Steam 开发人员网站上创建一个 API 令牌。
使用 steampy 库登录:使用 Steam 帐户和 API 令牌创建 steampy.SteamChat 对象,并调用其 login() 方法进行登录。
获取好友列表:使用 steampy.SteamChat 对象的 get_friends() 方法获取您的好友列表。
发送消息:使用 steampy.SteamChat 对象的 send_message() 方法向您的好友发送消息。
接收消息:使用 steampy.SteamChat 对象的 wait_event() 方法等待接收来自您的好友的新消息。
from steampy.client import SteamClient
from steampy.utils import get_friends
steam_client = SteamClient(api_key='your_api_key')
steam_client.login(username='your_steam_username', password='your_steam_password')
# 获取好友列表
friends = get_friends(steam_client)
# 向好友发送消息
friend_id = 'your_friend_steam_id'
message = 'Hello, friend!'
steam_client.chat.send_message(friend_id, message)
# 接收消息
while True:
event = steam_client.chat.wait_event()
if event.type == 'message' and event.steam_id == friend_id:
print(f'Message received from {friend_id}: {event.text}')
使用 steampy 库进行聊天,需要启用 Steam 帐户的“Steam Guard”功能,并使用移动身份验证器来生成身份验证代码。
参考GPT和自己的思路,要使用Python实现Steam消息收发,您可以使用以下步骤:
使用Steam账号和密码登录
获取Steam好友列表
选择要发送消息的好友
发送消息给所选好友
接收来自好友的消息
下面是一个基本的示例代码,演示如何使用steampy库中的send_message方法向好友发送消息:
import steampy
from steampy.client import SteamClient
from steampy.login import LoginCredentials
from steampy.exceptions import SteamError
# 登录信息
login = LoginCredentials(username='<steam账号>', password='<steam密码>')
# 创建 SteamClient 实例
client = SteamClient()
try:
# 登录到 Steam
client.login(login)
# 获取好友列表
friends = client.get_friends()
# 打印好友列表
print(friends)
# 选择好友发送消息
friend_id = '<好友Steam ID>'
message = 'Hello, friend!'
# 发送消息给所选好友
client.chat.send_message(friend_id, message)
# 接收来自好友的消息
while True:
message = client.chat.wait_for_new_messages()
print(message)
except SteamError as e:
print(e)
注意,您需要将<steam账号>和<steam密码>替换为您自己的Steam账号和密码,并将<好友Steam ID>替换为您要发送消息的好友的Steam ID。
如果对您有帮助,请给与采纳,谢谢。
以下答案基于ChatGPT与GISer Liu编写:
实现登录steam账号并进行消息收发需要用到一些库和API接口,下面是一个简单的示例,您可以根据您的需求进行修改和优化:
1.使用 requests 库进行登录,获取 cookies。
import requests
LOGIN_URL = "https://steamcommunity.com/login"
username = "your_username"
password = "your_password"
session = requests.Session()
login_data = {
"username": username,
"password": password,
"remember_login": "true",
"donotcache": str(int(time.time() * 1000)),
}
response = session.post(LOGIN_URL, data=login_data)
if response.status_code != 200 or "sessionid" not in session.cookies:
print("Login failed.")
else:
print("Login succeeded.")
2.使用 Steam 网络聊天接口发送消息。
import json
CHAT_URL = "https://steamcommunity.com/chat/message"
# 可以在 web chat 界面中查看好友 ID
friend_id = "12345678901234567"
message = "Hello, friend!"
chat_data = {
"text": message,
"chat_entry_type": "saytext",
"steamid_dst": friend_id,
"sessionid": session.cookies["sessionid"],
}
response = session.post(CHAT_URL, data=chat_data)
if response.status_code != 200:
print("Failed to send message.")
else:
print("Message sent.")
3.使用 Steam Web Chat 接口接收消息。
import websocket
WEBSOCKET_URL = "wss://web3-webchat-ec5-5-prd.steampipe.net/socket.io/?transport=websocket"
# 在 web chat 界面中打开开发者工具,找到 cookie 中的 steamLoginSecure 和 steamid
steam_login_secure = "your_steamLoginSecure_cookie"
steam_id = "your_steamid"
websocket.enableTrace(True)
ws = websocket.create_connection(WEBSOCKET_URL, cookie=f"steamLoginSecure={steam_login_secure}; steamid={steam_id}")
# 监听事件,包括消息、好友上线、好友下线等
while True:
result = ws.recv()
if result.startswith("0{\"chat_entry"):
message = json.loads(result[1:])["chat_entry"]["message"]
print(message)
请注意,上述代码仅仅是一个简单的示例,并不能保证完全正确。您需要根据实际情况进行修改和优化。同时,Steam 会定期更新接口,如果您的代码不能正常工作,请参考最新的文档