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

[HELP] Confused about chat:message content in the api

New to roll20. I'm trying to write some scripts triggered on specific chat content for my first campaign. I'm having trouble with the msg.content for msg type general. Is there any docs or examples or previous topics that would detail this construct/object? For the moment, I'm trying to capture the roll syntax sent from a character sheet button, (the line you can grab manually with a "previous command" uparrow in the chat box). When I dump the msg content, it seems to be already parsed, ergo no command syntax. Or I'm doing something stupid. Hints/point me in the right direction please? Apologies if there's some obvious docs I'm overlooking. (No problem when the msg type is rollresult. Problem with char sheet attacks -> they seem to be type general) I know some js, css and html, but don't program in these much. So speak slowly please.
Ok, my bad, I should have tried harder before posting. I can now see there is inline roll information in the msg.content, but where is the original text, as with the origRoll for rollresult messages? or can get the raw message text elsewhere? Any help appreciated.
1431052636
Lithl
Pro
Sheet Author
API Scripter
If a message contains inline rolls, the msg object will have an inlinerolls property containing the roll data for all inline rolls in the message. (msg.inlinerolls is undefined if there are no inline rolls in the message.) msg.inlinerolls will be an array, corresponding to the individual rolls in the message content. You can see the location each roll belongs is indexed with $[[0]] for the first roll, $[[1]] for the second, etc. forming a 1:1 relationship with the elements in the inlinerolls array.
Here's a snippet from my cron script to do the substitution Brian described: fixupCommand: function(cmd, inlineRolls){ function replaceInlines(s){ if (!inlineRolls){ return s; } var i = parseInt(s.substring(3, s.length - 2)); if ((i < 0) || (i >= inlineRolls.length) || (!inlineRolls[i]) || (!inlineRolls[i]['expression'])){ return s; } return "[[" + inlineRolls[i]['expression'] + "]]"; } return cmd.replace(/\$\[\[\d+\]\]/g, replaceInlines); }, You'd call it like "fixupCommand(msg.content, msg.inlinerolls || [])".
1431080598

Edited 1431094664
That looks like it should do it. Is there also a way to pull the original chat message text? If I up arrow in the message box, I get something like @{Character|Ability} Which text I would like to then capture and store for reuse. My first project is to provide a redo list like up arrow does, but in my version the redo list is specific to the selected token. Brian and manvetis info should let me do the equivalent, but if I had the orig message text I could mimic up arrow behavior exactly. I guess I could try to pull it out of the chat box using the DOM for the web page but that would be a total mess trying to inject code into the roll20 page and not forward compatible. Isn't there some way to grab this text directly out of the msg object, the chat box (after up arrow) or possibly roll20s command parser before its parsed? Thanks for the help!
1431098804
The Aaron
Pro
API Scripter
There is not. Consider a message like: !msg @{Rook|Intelligence} The message object looks like: { "content": "!msg 16", "playerid": "-JS3qKxIUPHLzSbK24ve", "type": "api", "who": "The Aaron (GM)" } The API does not receive the attribute reference. This is because the expansion of the attribute occurs very early in the order of operations on the Client side. Speaking of Client side, one of the hardest things for new API script writers to get a handle on is the Client-Server nature of the API. Your scripts execute in a Sandbox, an environment on the API server. The API Sandbox does not have a DOM, nor direct access to the various clients that are connected to the Campaign. The API Sandbox receives messages from the Campaign, via the event notifications, and has access to the state of the Campaign.
Thanks for the great info. I understand even if I could break out of the sandbox (and its VM) that would violate the license (not to mention every code of conduct I have heard of). You say “order of operations on the Client side”, but I am thinking I should not read this to mean the expansion is done on my browser/client. The dice engine must be on the server side, yes? And probably a shared dice engine outside the sandbox so roll20 guys can aggregate roll characteristics. (If msg expansion actually is happening my client, I could conceivably inject code into my client to trap it. I doubt I would take that on – I do have a day job - but it would be possible.) I guess I was holding out the very thin hope that the dev team had left a hook for the message expansion/parser inside the sandbox, such as a prototype I could override. It appears not. So what's left? The convoluted approach of up-arrow and grab the text from the chat box in the web-browser client-side's DOM. It's essentially code-injection on my browser again. Not only would that be client-browser specific, the dev team would have no reason to support it going forward. Plus, I would still have to somehow push the text I've now captured back through the roll20 API, to get it back into the sandbox so I can attach it to anything. I don't want to do that. An ugly mess. But I can do this: I can grab the inline rolls from the msg object inside the sandbox. I think. Even in your above example, any inline rolls for the attribute should show up in the msg object, right? For example, msg would include the rolls for: !msg @{Bugwort the Hairy|Some_attr_with_rolls} At least that's what I seem to be getting when I dump the msg objects. So the current plan is to play around with this approach and see if it has any usability advantage for the players. It is just frustrating when the text I want is taunting me from the chat box on the screen. LOOK! THERE IT IS! RIGHT NOW! I hit it with my magic +2 keyboard of agile programming, and it just LAUGHS AT ME!! I think this well is now officially dry. Unless you have a suggestion for me, Aaron. Or anyone. Or if you want to correct some misunderstanding I am displaying in my musing above, that would also be good. Anything you can do to keep me from hurting myself is always appreciated.
1431133357
The Aaron
Pro
API Scripter
Right the order of operations applies to the parsing of the command on the client before it is sent to the server. Attributes are replaced with what the store. See the difference between my original command (with the attribute reference) and the msg.content that the API received (where it has 16).
1431133648
The Aaron
Pro
API Scripter
If you don't mind adding an extra step, you could use API Buttons to get the information sent to the server: (Note that you have to use the HTML Entity notation for the @ sign) [save](!msg @{Rook|intelligence} ) The API sees this message as: { "content": "[save](!msg @{Rook|intelligence} )", "playerid": "-JS3qKxIUPHLzSbK24ve", "type": "general", "who": "The Aaron (GM)" } The client is sent a command button named 'save' that issues the command.
OK, that's interesting. I couldn't figure out how to suppress expansion, so the HTML codes will do it. Or delay it for one pass through the expansion process. Is there any way to apply the roll template in an api script? I'm now trying to reproduce the effect of the last character|attr command using the inline roll info from the msg object members, but i guess i need to apply the roll template somehow if I want it to look the same as the original chat:message. Maybe there is a script done for this, but I didn't see anything.
OK, looks like that was a noob question. The & syntax for templates is right in the docs. Apologies for not doing my homework.
1431186538
The Aaron
Pro
API Scripter
No problem. :)
OK, so I expect to finish banging out my script after supper tonight. Got extra private JS advice from Aaron too. What's the protocol here to recruit alpha/beta testers? Can I just post the source on a new topic in this forum and ask for feedback? Or do I have to do a pull request on the github page?
1431721752

Edited 1431721778
Lithl
Pro
Sheet Author
API Scripter
Unless the script is relatively short, I wouldn't recommend posting the entirety of the source in your thread, but linking to it is fine. There are some popular scripts which aren't in the repo at all, like power cards. (For some authors, this is due to the licensing requirements for scripts in the repo.)
1431722135
The Aaron
Pro
API Scripter
github gists work well: <a href="https://gist.github.com/" rel="nofollow">https://gist.github.com/</a>
Brian said: Unless the script is relatively short, I wouldn't recommend posting the entirety of the source in your thread, but linking to it is fine. There are some popular scripts which aren't in the repo at all, like power cards. (For some authors, this is due to the licensing requirements for scripts in the repo.) I'm just too lazy to deal with git and all that...
1431722479
The Aaron
Pro
API Scripter
HoneyBadger said: Brian said: Unless the script is relatively short, I wouldn't recommend posting the entirety of the source in your thread, but linking to it is fine. There are some popular scripts which aren't in the repo at all, like power cards. (For some authors, this is due to the licensing requirements for scripts in the repo.) I'm just too lazy to deal with git and all that... I think he's talking about Ken. L. =D
Yeah, I wasn't keen on what I saw of the license myself. Probably I'll set up my own github repo. i think I remember how...
1431726149
The Aaron
Pro
API Scripter
It did get changed to the MIT license at some point. Not sure if people find that objectionable or not.
I'm not worried about the license. They keep borrowing my ideas anyway, lol.
1431726338
The Aaron
Pro
API Scripter
=D
1431728550
Gen Kitty
Forum Champion
*bats at HB* If your Powercards script isn't in the Official Repository when they finally get the system done for auto-deploying of scripts&updates, I'm going to have to sniffle on you. As frequently as Powercards gets updated, and as often as I have to ask my GMs to update their copies... :&gt;
GenKitty said: *bats at HB* If your Powercards script isn't in the Official Repository when they finally get the system done for auto-deploying of scripts&updates, I'm going to have to sniffle on you. As frequently as Powercards gets updated, and as often as I have to ask my GMs to update their copies... :&gt; I'm actually waiting for the official repository to have the auto-deployment set up before I include PowerCards. I'm also not planning on any more features after conditionals and variables make it in. So it'll just be bug fixes after that I hope.
1431747060
Lithl
Pro
Sheet Author
API Scripter
The Aaron said: It did get changed to the MIT license at some point. Not sure if people find that objectionable or not. It changed from GPL to MIT... 7 days after the repo was created in the first place... which was 2 days before Steven told us of its existence. As far as script writers are concerned the repo has always been MIT.
1431747292
The Aaron
Pro
API Scripter
Ha Brian, fine. =D