Jessecar96 / SteamBot

Automated bot software for interacting with Steam Trade
http://scrap.tf
MIT License
1.33k stars 910 forks source link

TradeException #375

Closed jtcd closed 11 years ago

jtcd commented 11 years ago

Hi!

Whenever I start a Bot I get 1 of 2 outcomes.

Outcome 1: It works perfectly indefinitely

Outcome 2: This exception is thrown every time a trade session is started:

TradeException ("The web command to send the trade message failed.");

The problem originates from this method:

        bool SendMessageWebCmd (string msg)
        {
            var data = new NameValueCollection ();
            data.Add ("sessionid", sessionIdEsc);
            data.Add ("message", msg);
            data.Add ("logpos", "" + LogPos);
            data.Add ("version", "" + Version);

            string result = Fetch (baseTradeURL + "chat", "POST", data);

            Console.WriteLine(result);

            dynamic json = JsonConvert.DeserializeObject(result);

            if (json == null || json.success != "true")
            {
                return false;
            }

            return true;
        }

json.success is false. I tried to look for the error by printing the entire serialized json string but is has only 1 element which is "success" = "false".

I am able to log in to steam and trade normally through the official steam client. I am using visual studio express 2012. Sorry in advance if this is a noob question. I'm just curious if anyone else has had this issue or knows how to resolve it. Thanks!

iMagooo commented 11 years ago

Mine seems to throw these exceptions quite a lot also (trade message, trade accept, set trade ready etc.)

It's on my list of things to investigate. I'll chime in if I find anything helpful.

jtcd commented 11 years ago

@iMagooo Okay thanks!

I think I have a lead anyway. I have just added a method for sending group invites to friends. It works whenever i get the indefinitely perfect outcome and does not work when the TradeException gets thrown every trade.

The web request to send group invites to friends returns the result "Missing or invalid form session key" when it fails.

Seems like there is something wrong with the session key.

waylaidwanderer commented 11 years ago

It's because you're missing the session key :P Check out https://github.com/waylaidwanderer/SteamGrouper - it may be helpful. Lemme read through my code again and find the relevant section for you.

Edit: Also, TradeException being thrown has nothing to do with your sending group invite to friends. TradeException is thrown when a trade error occurs. It's more likely that you're not doing the proper cleanup when this happens (aka not catching it and handling it) and the bot gets stuck and doesn't respond.

waylaidwanderer commented 11 years ago

Here is the function I wrote that invites users to groups. https://github.com/waylaidwanderer/SteamGrouper/blob/master/SteamBot/Bot.cs#L708

jtcd commented 11 years ago

@waylaidwanderer

Thanks a lot for the tips :)! I am not missing the session key in my invite to group method though, I know this because the method works normally when i get outcome 1 from above - "Bot works perfectly indefinitely". With regard to the TradeException problem I have tried catching the exception and recursively retrying the process but to no avail.

waylaidwanderer commented 11 years ago

Perhaps OnTradeClose() isn't being called and thus the bot is getting stuck?

Try running a loop that continually checks if Bot.CurrentTrade is null, and if it is, fires the OnTradeClose() function after the trade has initialized.

jtcd commented 11 years ago

Hmm the problem starts from the very first trade. I've looked at your suggestion and UserHandler.OnTradeClose calls Bot.CloseTrade which returns immediately if Bot.CurrentTrade == null.

public void CloseTrade() 
        {
            if (CurrentTrade == null)
                return;
         ...
        }

Did I misunderstand you x)?

I have just tried using a fresh clone of the repository with VS 2010 .Net4.0 and I get the same problem. I was getting the same problem in VS 2012 using a fresh clone of the repository. Does anyone else get the same problems using fresh clones?

iMagooo commented 11 years ago

I've got a timer which starts in OnTradeInit(), has an interval of 30 seconds and checks if the bot is listed as busy, while Bot.CurrentTrade == null.

If true, OnTradeClose() is called.

I find that option better than a continuous loop, the bot can only be stuck in Busy status if the bot was traded, so there's no need to check Bot.CurrentTrade if it hasn't been in a trade.

This is a temporary fix, of course, until I work out a solution for the exceptions.

waylaidwanderer commented 11 years ago

This is my function:

private void CheckTradeState()
{
    while (true)
    {
        if (Bot.CurrentTrade == null && !tradeClosed)
        {
            OnTradeClose();
            checkTrade.Abort();
            break;
        }
        System.Threading.Thread.Sleep(800);
    }
}

It polls every 800 milliseconds. checkTrade is a Thread, which I use by checkTrade = new Thread(CheckTradeState); in OnTradeInit().

jtcd commented 11 years ago

I rectified my problem by validating the session id whenever i start a Bot. Not a clean solution but my Bots have not thrown a single exception for the last 4 days. Thanks everyone for the help :)!