Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Newbie looking for Help with passing parameter to API

I am new to Roll20 and the API. I have some familiarity with javascript and html, and quite a bit of experience with other coding languages. But I am struggling to send a parameter to the API. What I want to do is pass the current characters ability Strength to the API so that I can perform calculations, make a random roll and then return the result via the sendChat() function. I would like to call it using something like   !strengthContest @{selected|Strength} The function I have written does work if I call it using !strengthContest   and have a fixed value for strength set in the API code. I am currently using if('api'===msg.type && /^!strengthContest(\b\s|$)/i.test(msg.content)){       myFunc(msg.who); } Where myFunc does all the processing and returns the result. Can anyone point me to the right place to see howto break down the command line to get the value. or how to call the value from the API. I did see this one in a post, but after an hour searching I cant find it again. I have found several posts which call the ability value by using the character name, but again i can't work out how to use this, as it could be different characters calling on the function, and All i have seen was using specific character info. Before anyone states I haven't posted all the code. I am not looking for anyone to fix the problem for me I just want to be pointed in the right direction as to where i should be looking/ what i should do. Many thanks
1621037424
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Welcome to Roll20 and the API Martin! It depends on how you want to handle it. You could do just as you outlined and put the strength value in the chat message that triggers the API. const myFunc = function(who,val){ sendChat(who,`[[${val}*8]]`); }; on('chat:message',(msg_orig)=>{ let msg = _.clone(msg_orig);//I do this to avoid corrupting the chat message for other scripts. if(/^!strengthContest(\s+)/i.test(msg.content)){//you don't really need the msg.type check since you're already checking the start of msg.content for !... let value = msg.content.replace(/^strengthcontest\s+(\d+).*/,'$1'); myFunc(msg.who,value); } }); however if you need to get any other information from the character in question, this won't really give you anything else, unless you start tacking on arguments to the chat message. You could instead pass a character id or use the selected array of the chat message to look for a character represented by a selected token to then use in the API to get the character object for, or attributes associated with that character. const myFunc = function(who,id){ let character = getObj('character',id); let attributes = findObjs({type:'attribute',characterid:id}); //Do calculations with the values of the attributes and store them in val sendChat(who,`[[${val}*8]]`); }; on('chat:message',(msg_orig)=>{ let msg = _.clone(msg_orig);//I do this to avoid corrupting the chat message for other scripts. if(/^!strengthContest(\s+)/i.test(msg.content)){//you don't really need the msg.type check since you're already checking the start of msg.content for !... let id = msg.content.replace(/^strengthcontest\s+([^\s]+).*/,'$1'); myFunc(msg.who,id); } }); Hope that helps
1621037964
The Aaron
Roll20 Production Team
API Scripter
What Scott said, save that I still do the check against type as it eliminates non-API messages much faster than the Regex.  =D
1621039736
The Aaron
Roll20 Production Team
API Scripter
Just to add to that, The API gets a message which contains various things, but the part you're mostly interested in is the message's content property.  That will contain the parsed text that was put in chat.  At its simplest, it will literally be what was typed: !test-api Well result in a msg.context of: !test-api However, it starts to get more complicated as you add other things.  Somethings that are important to know are: Roll Queries and Attribute references will be replaced with their resultant values. Inline rolls will be replaced with a placeholder referencing the roll in the msg.inlinerolls object. !test-api ?{Which|left|right} @{Keiara|hp} [[1d6+3]] { "content": "!test-api left 11 $[[0]]", "inlinerolls": [ { "expression": "1d6+3", "results": { "resultType": "sum", "rolls": [ { "dice": 1, "results": [ { "v": 6 } ], "sides": 6, "type": "R" }, { "expr": "+3", "type": "M" } ], "total": 9, "type": "V" }, "rollid": "-M_hUTvViBddgi6jihK2", "signature": "65d83c788aafc73881e1d7d0663ceeecbd2c9dc2fae7f1a92efa16b3fb05f9f19c2ca49c2b7f43765a8e37956b0b5559193337691921ed17abb769b204f455c9" } ], "playerid": "-MA-6LlhK58UhtPa2ib7", "type": "api", "who": "TheAaron (GM)" } The msg.content just needs to be parsed as you would parse any string.  Generally, I just split it on 1 or more spaces and deal with each argument in sequence, or something more complicated. let args = msg.content.split(/\s+/).slice(1);
1621041276
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
The Aaron said: What Scott said, save that I still do the check against type as it eliminates non-API messages much faster than the Regex.  =D And, TIL
1621049708
timmaugh
Forum Champion
API Scripter
BTW, here is a write up I did of developing a plan for dealing with the construction of your expected command line: <a href="https://app.roll20.net/forum/permalink/9049444/" rel="nofollow">https://app.roll20.net/forum/permalink/9049444/</a> It was in response to someone with a specific usage they were going for, but the discussion is generic enough to maybe be of some help.
1621064671

Edited 1621080924
Thanks everyone form the rapid responses. this gives me plenty to look at and work on. &lt;update &gt; so after a couple of hours I have sorted it out, and think I’m beginning to understand some of the api interface. once again many thanks