Python. Create a Steam bot.

Preface.

In this tutorial you will learn how to work with the steampylibrary, where you can create a steam bot.

Preparation

First of all, get steam_api key, we will need it to get information about trades. You can get it on the website steam: https://steamcommunity.com/dev/apikey

Next, you will need shared_secret and identity_secretthat give full access to your account.

You can get it in the SDA in the maFilesfolder, provided that you have not encrypted this file. Or on a phone running android with root rights, to which steamis tied.

Create a text file steam_guard.json, and place the following text there.

{
    "steamid": "YOUR_STEAM_ID_64",
    "shared_secret": "YOUR_SHARED_SECRET",
    "identity_secret": "YOUR_IDENTITY_SECRET"
}

Steamid take it here. In the site field, enter the link to the account.

And install the steampylibrary by entering this command in the console.

pip install steampy

We confirm the exchange.

Let's write the basis of a steam bot.

from steampy.client import SteamClient #импортируем library

steam_client = SteamClient('MY_API_KEY') 
steam_client.login('MY_USERNAME', 'MY_PASSWORD', 'steam_guard.json') #авторизируемся in your account

This code is responsible for authorization on the account.

Next, ask a friend or bot to throw a tradeso that an unrecessed exchange is hanging on the account.

And follow this link.

https://api.steampowered.com/IEconService/GetTradeOffers/v1/?key=STEAM_API_KEY&get_received_offers=1

STEAM_API_KEY – replace with your own.

This link is an api query that displays information about incoming trades in json format, more here.

We, in all this mess, are only interested in tradeofferid. You will need it to confirm the exchange. Now, copy the following lines into your script.

trade_id = yours_trade_id
steam_client.accept_trade_offer(trade_id)

trade_id – replace with your own.

And run the script.

As a result, the exchange was accepted without our participation, which is cool.

Throw the exchange.

Create a new script, and put the following code there:

from steampy.client import SteamClient, Asset
from steampy.utils import GameOptions, get_key_value_from_url, account_id_to_steam_id

def find_item_in_inventory(item_hash_name, items):
    for item in items.values():
        market_hash_name = item['market_hash_name']
        if market_hash_name != item_hash_name:
            continue
        return {
            'market_hash_name': market_hash_name,
            'id': item['id']
        }

def make_trade_1_item(give_item, get_item, trade_link):
    game = GameOptions.TF2
    my_items = steam_client.get_my_inventory(game)
    my_item_give = find_item_in_inventory(give_item, my_items)
    my_asset = [Asset(my_item_give['id'], game)]

partner_account_id = get_key_value_from_url(trade_link, 'partner', True)
    partner_steam_id = account_id_to_steam_id(partner_account_id)
    partner_items = steam_client.get_partner_inventory(partner_steam_id, game)
        
partner_item_give = find_item_in_inventory(get_item, partner_items)
    partner_asset = [Asset(partner_item_give['id'], game)]

steam_client.make_offer_with_url(my_asset, partner_asset, trade_link)

steam_client = SteamClient('secret')
steam_client.login('secret', 'secret', "steam_guard.json")
trade_link = 'https://randomSteamTradeOfferLink1234_under_prog_ru'
make_trade_1_item('Refined Metal', 'Refined Metal', trade_link)

For sending the trade is responsible for the function make_trade_1_item,let's analyze it.

game = GameOptions.TF2

TF2 inventory was chosen as inventory

my_items = steam_client.get_my_inventory(game)
my_item_give = find_item_in_inventory(my_items, give_item)

We get information about inventory items. And weed out the unnecessary, using the function find_item_in_inventory. As a result, we get a dictionary of this format:

{
  'market_hash_name': item_name,
  'id': id_ subject
}

Next, pack the item in Asset.

my_asset = [Asset(my_item_give['id'], game)]

Asset takes 2 arguments:

  • item id
  • game code (for TF2,it's 440)

Our items are ready, it remains to prepare the partner'sitems. First, we will get the partner's inventory.

partner_account_id = get_key_value_from_url(trade_link, 'partner', True) #получаем account id from the trade link
partner_steam_id = account_id_to_steam_id(partner_account_id) # account id translate into steam_id
partner_items = steam_client.get_partner_inventory(partner_steam_id, game) #получаем partner inventory

We'll weed out everything unnecessary, and throw in Asset.

partner_item_give = find_item_in_inventory(get_item, partner_items)
partner_asset = [Asset(partner_item_give['id'], game)]

Assets collected,time to send trade.

steam_client.make_offer_with_url(my_asset, partner_asset, trade_link)

Conclusion.

This library, I was very helpful in writing the tf2.tm bot I mentioned here. By applying the knowledge from this article from here and here,you can write a similar bot.

Final result.