Jump to content
McKay Development

Proper way of setting cookies for controlling bot by POST requests


Fabro
 Share

Recommended Posts

Hello. I am quite new to Node.JS so I need a little help sorting some things out.

I am trying to make the bot controlled by POST request by using Express for the URL routing, so the bot only responds to specific requests made from specific domain to specific endpoints. I am able to login to SteamUser, but I am not able to share the cookies to SteamCommunity.

I wasn't able to find a way to keep the bot logged in across different endpoints, I am assuming that it comes from the fact that every request is unique and they are not sharing data between them, so, I have to make the bot log in on each request, and that's fine with me. The problem comes when I try to share the cookies from webSession event, and send a trade on the loggedOn event. It appears that when logged on event fires, cookies are not yet configured. What approach should I use to perform the login on the SteamCommunity module?

Here is my current code, that rejects my trade offer, because cookies are not yet configured.

app.get('/lero', async (req, res) => {

    res.send('Loaded');

	const SteamUser = require('steam-user');
	const SteamCommunity = require('steamcommunity');
	const SteamTotp = require('steam-totp');
	const TradeOfferManager = require('steam-tradeoffer-manager');
	const FS = require('fs');
	
	let client = new SteamUser();
	let manager = new TradeOfferManager({
		"steam": client,
		"domain": "bot.test",
		"language": "es"
	});
	let community = new SteamCommunity();
	
	// Steam logon options
	let logOnOptions = {
		"accountName": "censored",
		"password": "censored",
		"twoFactorCode": SteamTotp.getAuthCode("censored"),
	};
	
	if (FS.existsSync('polldata.json')) {
		manager.pollData = JSON.parse(FS.readFileSync('polldata.json').toString('utf8'));
	}
	
	client.logOn(logOnOptions);

	const { Sequelize, Op, Model, DataType, DataTypes } = require('sequelize');
	const sequelize = new Sequelize('cendsored', 'censored', 'censored', {
		host: 'localhost',
		dialect: 'mysql',
		define: {
			underscored: true,
			createdAt: 'created_at',
			updatedAt: 'updated_at'	,
			deletedAt: 'deleted_at',
		}
	  });
	  
	const initModels = require('./models/init-models');
	const models = initModels(sequelize);

	client.on('webSession', (sessionID, cookies) => {
		community.setCookies(cookies);
		manager.setCookies(cookies);
		console.log(cookies); // console logs this after the error, meaning trade is sent before this
	});
	
	client.on('loggedOn', async () => {
		console.log("Logged into Steam");
		const su = await models.steam_users.findOne({
			where: {
				steam_personaname: 'censored',
			},
			include: [{
				model: models.users,
				as: 'user',
			}],
		}).then((r) => {
			client.chat.sendFriendMessage(r.steam_steamidmodern, "Sending trade in a moment");
			let offer = manager.createOffer('censored');
			console.log(offer);
			console.log(community);
			let item = {
				assetid: 'censored',
				appid: '730',
				contextid: '2',
				amount: 1,
			};

			if (offer.addTheirItem(item)) {
				offer.send((err, status) => {
					if (err) {
						console.log(err);
					} else {
						console.log(status);
					}
				});
			}
		});
	});
});

I am familiar with events, since in PHP I have worked with OpenCart, Wordpress and Laravel, but I cannot find the right order of events here, to correctly log in both modules.

Any help will be very much appreciated.

Link to comment
Share on other sites

webSession will be emitted some time after loggedOn. loggedOn is the first event to be emitted after you log on; everything follows from there. Send your trade inside the webSession event and you should be good to go.

Also, there's nothing that says you couldn't keep the bot logged on between requests, avoiding the need to log back on every time.

Link to comment
Share on other sites

14 hours ago, Dr. McKay said:

webSession will be emitted some time after loggedOn. loggedOn is the first event to be emitted after you log on; everything follows from there. Send your trade inside the webSession event and you should be good to go.

Also, there's nothing that says you couldn't keep the bot logged on between requests, avoiding the need to log back on every time.

Hello. Thank you for your help. I have moved the offer inside webSession and now the error has gone, nonetheless, I am waiting the 7 days cooldown because of new device, but I think it will work as expected inside webSession.

About keeping it logged, the few times I have tested trying to keep the bot logged, it randomly asked for SteamGuard code in the console, but it might be something I messed up because of me not being familiar with the events and the order they are fired, since there are events concerning keeping the bot logged. I have searched the wiki, but I wasn't able to find a "list" of events in order of firing. I will keep digging the source code and look for them and try to understand better the way they work, I am doing that but since I am new to Node.JS it takes me a while to understand the syntax. By they way, can you listen an event, inside an already listened event? For instance, can you listed for a chat message inside the callback of listened to a offer recieved? Or events cannot be nested inside each other?

Once again, thank you for your help.

Link to comment
Share on other sites

There's no list of the order that events get emitted in because it isn't really defined. loggedOn happens first, unless steamGuard happens before you log on. Once you're logged on, everything else happens as Steam sees fit. Obviously, once you're disconnected (disconnected and error events) then further events stop.

You could add an event listener inside of another event listener, but unless you use user.once or manually remove the listener every time, then your event handler will start getting called multiple times when the event fires.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...