ChatWars API NodeJS wrapper and REST API HTTP server
NodeJS interface
This project provides promise-based ChatWars interface as NPM library.
A sample of the NodeJs library usage is demonstrated in alevinru/CWClientHelperAppBot Telegram bot project.
If you are not interested in utilising REST API, then you could go straight to the sample bot project, and do not setup the REST server described below. However, in order to serve the bot you'll have to provide some env variables declared below in the setup section.
Shell
npm i --save cw-rest-api
NodeJS
node-amqp-connection-manager is used to provide AMQP node reconnection.
import CWExchange from 'cw-rest-api'; // Assuming you've already done user authorization and got tokenconst USER_ID = parseInt(process.env.USER_ID, 0);const USER_TOKEN = process.env.USER_TOKEN; const cw = new CWExchange(); // optional params { appName, timeOut }const manager = cw.connect(); // optional params { apiUrl, accessToken, amqpProtocol } // manager is an instance of amqp-connection-manager manager.on('connect', async () => { try { const profile = await cw.requestProfile(USER_ID, USER_TOKEN) console.log(profile); const stock = await cw.requestStock(USER_ID, USER_TOKEN); console.log(stock); // as a promise without await cw.wantToBuy(USER_ID, { itemCode: '07', quantity: 12, price: 1 }, USER_TOKEN) .then(deal => console.log(deal)) .catch(errorText => console.error(errorText)); } catch (e) { console.error(e); } });
ChatWars REST API HTTP server
Install
git clone git@github.com:alevinru/CWClient.git cd CWClient
Setup
Since you've got managed to request and obtain valid CW API credentials you must have username
and password
.
It is required to set them into the environment variables named respectively as CW_APP_NAME
and CW_APP_PASSWORD
.
export CW_APP_NAME=usernameexport CW_APP_PASSWORD=password
By default, http server starts on port 8888 and connects to CW3 api instance. This is configurable with environment variables for which default values are provided in the bundled nodemon.json file.
Run
npm run start
Upon success you need to get a valid CW usesId
and ask that user for an authorization doing:
http POST localhost:8888/api/auth/101010101
Then CW Telegram Bot sends an authorization request message to the user Telegram account
Code 11223344 to authorize aliovin_CWClientAppBot. This app will have the access to:
- read your profile information
- issue a wtb/wts/rm commands on your behalf
- read your stock info
Now you need to use this code
to get user token
:
http POST /api/token/101010101/11223344
If everything's fine you would get a response like that:
{
"id": "1a2346c48ee342367d0e6af3c155b744",
"token": "0c32d64ac6f348cfae7e441f317fd898",
"userId": 101010101
}
You should remember the user token and use it for accessing user data methods:
export USER_TOKEN=0c32d64ac6f348cfae7e441f317fd898
Now you should be able to use all the rest of the API methods passing the received token as a http header.
http POST /api/profile/101010101 authorization:$USER_TOKEN
REST API Methods
POST /api/auth/:userId
POST /api/token/:userId/:authCode
GET /api/profile/:userId
Send authorization token in Authorization: http header
GET /api/stock/:userId
Send authorization token in Authorization: http header
GET /api/info
POST /api/buy/:itemCode?:userId&:quantity&:price
Send authorization token in Authorization: http header
Method does exactMatch:true
requests
⚠️ If you regularly do a noticeable amount of over-the-marked priced buys they would revoke CW API credentials from you
Sample result assuming quantity is 1 and itemCode is 07
HTTP Error responses
HTTP/1.1 404 Not Found
Content-Length: 20
Content-Type: text/plain; charset=utf-8
NoOffersFoundByPrice
HTTP/1.1 404 Not Found
Content-Length: 10
Content-Type: text/plain; charset=utf-8
NoSuchUser
HTTP/1.1 401 Unauthorized
Content-Length: 11
Content-Type: text/plain; charset=utf-8
InvalidCode
HTTP/1.1 401 Unauthorized
Content-Length: 12
Content-Type: text/plain; charset=utf-8
InvalidToken
HTTP/1.1 502 Bad Gateway
Content-Length: 12
Content-Type: text/plain; charset=utf-8
BattleIsNear
HTTP/1.1 502 Bad Gateway
Content-Length: 10
Content-Type: text/plain; charset=utf-8
UserIsBusy
HTTP/1.1 404 Not Found
Content-Length: 17
Content-Type: text/plain; charset=utf-8
InsufficientFunds