How do I pass connection-time websocket parameters to Phoenix from Elm? - websocket

I was following along Programming Phoenix but using Elm for my front end, rather than Javascript. The second part of that book describes how to use websockets. The book's running example has you create an authentication token for the client side to pass to Phoenix at connection creation time. The Javascript Socket class provided with Phoenix allows that, but there's no obvious way to do it in Elm (as of 0.17 and the date of this question).

As in the book, make the token visible to Javascript by attaching it to window.
<script>window.auth_token = "<%= assigns[:auth_token] %>"</script>
In web/static/js/app.js, you'll have code that starts Elm. Pass the token there.
const c4uDiv = document.querySelector('#c4u-target');
if (c4uDiv) {
Elm.C4u.embed(c4uDiv, {authToken: window.auth_token});
}
On the Elm side, you'll use programWithFlags instead of program.
Your init function will take a flags argument. (I'm using the Navigation library for a single-page app, which is why there's a PageChoice argument as well.)
type alias Flags =
{ authToken : String
}
init : Flags -> MyNav.PageChoice -> ( Model, Cmd Msg )
Within init, tack on the token as a URI query pair. Note that you have to uri-encode because the token contains odd characters. Here's the crude way to do that. Note: I am using the elm-phoenix-socket library below, but the same hackery would be required with others.
let
uri = "ws://localhost:4000/socket/websocket?auth_token=" ++
(Http.uriEncode flags.authToken)
in
uri
|> Phoenix.Socket.init
|> Phoenix.Socket.withDebug
|> Phoenix.Socket.on "ping" "c4u" ReceiveMessage

I got here by a Tweet by Brian, about encoding from Elm.
In this case I like to handle it from the JavaScript side.
I tried to replicate the way the Phoenix client sets it up.
Instead of passing the token I passed the complete endpoint...
I've put the token in JSON a hash
<script id="app-json" type="application/json"><%= raw #json %></script>
Which I read on the client, and pass to the Elm embed
var data = JSON.parse(document.getElementById("app-json").innerHTML)
var token = encodeURIComponent(data.token)
var elm = window.Elm.App.embed(document.getElementById("elm-container"), {
socketEndpoint: "ws://" + window.location.host + "/socket/websocket?token=" + token
})

Related

QuerySchedule WebAPI function call

I am using Web API calls to Dynamics 365 to get result for function QuerySchedule. I have tried using this as a bound function as well. But none of them returns the expected result. Below is documentation on this:
https://learn.microsoft.com/en-us/dynamics365/customer-engagement/web-api/queryschedule?view=dynamics-ce-odata-9
I have tried different resource id, different ways to specify the enum type in the call, fully qualified function name, etc but I always get error.
Following is my call:
https://mycrm.com/api/data/v9.0/QuerySchedule(ResourceId=#p1,Start=#p2,End=#p3,TimeCodes=#p4)?#p1=resourceguid&#p2=2019-01-05T09:27:39Z&#p3=2019-01-05T21:27:39Z&#p4=Available
The output is expected to be QueryScheduleResponse as mentioned in below link:
https://learn.microsoft.com/en-us/dynamics365/customer-engagement/web-api/queryscheduleresponse?view=dynamics-ce-odata-9
But I keep getting error message:
Object reference not set to an instance of an object.
Could anyone who has done web api calls to Dynamics 365 using OData or has any experience with this kindly help?
Quickly did a browser console test, the code snippet from this blog post works fine.
Pasting part of the snippet from the blog, your code may break as you are passing "Available" instead of ['0'] for TimeCodes.
var requestUrl = "/api/data/v9.0/QuerySchedule(ResourceId=#p1,Start=#p2,End=#p3,TimeCodes=#p4)";
requestUrl += "?#p1=" + context.getUserId().replace("{", "").replace("}", "");
//put Id of resource you want get data for as parameter 1
requestUrl += "&#p2=" + JSON.stringify(start).replace(/"/g, "");
requestUrl += "&#p3=" + JSON.stringify(end).replace(/"/g, "");
requestUrl += "&#p4=" + JSON.stringify(['0']);
Even you can paste this url in your browser address bar to smart test:
Request:
https://test.crm.dynamics.com/api/data/v9.0/QuerySchedule(ResourceId=#p1,Start=#p2,End=#p3,TimeCodes=#p4)?#p1=0EEE678F-C4FF-E711-A959-000D3A1A941E&#p2=2019-01-15T09:27:39Z&#p3=2019-01-15T21:27:39Z&#p4=['0']
You may notice ['0'] turn into [%270%27] but the below expected response will appear.
Response:
{"#odata.context":"https://test.crm.dynamics.com/api/data/v9.0/$metadata#Microsoft.Dynamics.CRM.QueryScheduleResponse","TimeInfos":[{"Start":"2019-01-15T00:00:00Z","End":"2019-01-16T00:00:00Z","TimeCode":"Available","SubCode":"Schedulable","SourceId":"15f40c32-1609-46db-93da-1bbb8eb19c9d","CalendarId":"69b0ee2b-bad7-4b8e-9bcc-d03e76b45a03","SourceTypeCode":4004,"IsActivity":false,"ActivityStatusCode":-1,"Effort":1.0,"DisplayText":""}]}

Using JobQueue to continuously refresh a message

I'm building a Telegram bot that uses ConversationHandler to prompt the user for a few parameters and settings about how the bot should behave. This information is stored in some global variables since it needs to be available and editable by different functions inside the program. Every global variable is a dictionary in which each user is associated with its own value. Here's an example:
language = {123456: 'English', 789012: 'Italian'}
where 123456 and 789012 are user ids obtained from update.message.from_user.id inside each function.
After all the required information has been received and stored, the bot should send a message containing a text fetched from a web page; the text on the web page is constantly refreshed, so I want the message to be edited every 60 seconds and updated with the new text, until the user sends the command /stop.
The first solution that came to my mind in order to achieve this was something like
info_message = bot.sendMessage(update.message.chat_id, text = "This message will be updated...")
...
def update_message(bot, update):
while True:
url = "http://example.com/etc/" + language[update.message.from_user.id]
result = requests.get(url).content
bot.editMessageText(result, chat_id = update.message.chat_id, message_id = info_message.message_id)
time.sleep(60)
Of course that wouldn't work at all, and it is a really bad idea. I found out that the JobQueue extension would be what I need. However, there is something I can't figure out.
With JobQueue I would have to set up a callback function for my job. In my case, the function would be
def update_message(bot, job):
url = "http://example.com/etc/" + language[update.message.from_user.id]
result = requests.get(url).content
bot.editMessageText(result, chat_id = update.message.chat_id, message_id = info_message.message_id)
and it would be called every 60 seconds. However this wouldn't work either. Indeed, the update parameter is needed inside the function in order to fetch the page according to the user settings and to send the message to the correct chat_id. I'd need to pass that parameter to the function along with bot, job, but that doesn't seem to be possible.
Otherwise I would have to make update a global variable, but I thought there must be a better solution. Any thoughts? Thanks.
I had the same issue. A little digging into the docs revealed that you can pass job objects a context parameter which can then be accessed by the callback function as job.context.
context (Optional[object]) – Additional data needed for the callback function. Can be accessed through job.context in the callback. Defaults to None
global language
language = {123456: 'English', 789012: 'Italian'}
j=updater.job_queue
context={"chat_id":456754, "from_user_id":123456, "message_id":111213}
update_job = job(update_message, 60, repeat=True, context=context)
j.put(update_job, next_t=0.0)
def update_message(bot, job):
global language
context=job.context
url = "http://example.com/etc/" + language[context["from_user_id"]]
result = requests.get(url).content
bot.editMessageText(result,
chat_id = context["chat_id"],
message_id = context["message_id"])

how to send regex to server side in meteor

In my application, I'm building a query object some thing like below
Object {pointType: /analog/i, _id: Object}
I tried to store it in session variable,
Session.set("currentPointsQueryObject",queryObj);
Then on click event I'm getting this object
var res= Session.get("currentPointsQueryObject");
console.log(res);
but here I'm getting like below
Object {pointType: Object, _id: Object}
Meanwhile, I sent group_id to the server
by geting it from session variable like
var group_id=Session.get("currentGroupId");
which is working fine(it is displaying id in server log)
Then, I've tried storing it in global variable, which returning as expected
like below on click event
Object {pointType: /analog/i, _id: Object}
but when I sent it to server side method (Immediate line after console.log() )
Meteor.call("updateGroupPoints",res,function(err,data){
console.log("updated points");
console.log(data);
});
when I log res in server console, it is showing
{ pointType: {}, _id: { '$nin': [] } }
Althoug I have something in pointType, It is not passed to the server.
Anyone had idea, Is this the thing related storing?
You cannot directly serialize RegExp to EJSON, but you can:
var regexp = /^[0-9]+$/;
var serialized = regexp.source;
Send serialized and then deserialize:
new RegExp(serialized)
Take a look at : Meteor: Save RegExp Object to Session
/analog/i is a regular expression, right? Values stored in Session and values sent to methods must be part of EJSON values. Regular expression aren't.
There's a handy way to teach EJSON how to serialize/parse Regular Expressions (RegExp) as of 2015 documented in this SO question:
How to extend EJSON to serialize RegEx for Meteor Client-Server interactions?
Basically, we can extend the RegExp object class and use EJSON.addType to teach the serialization to both client and server. Hope this helps someone out there in the Universe. :)
Simply stringify your RegExp via .toString(), send it to the server and then parse it back to RegExp.

Need assistance with unfamiliar syntax, error - e is undefined - Google Apps Script(GAS)

I'm using a script exactly like the one on the tutorial here, https://developers.google.com/apps-script/reference/ui/file-upload
However, despite using the syntax I keep getting e is undefined in the statement:
var fileBlob = e.parameter.dsrFile;
I think that means my function doPost(e) is probably wrong somehow. Here is my entire script below.
// Create Menu to Locate .CSV
function doGet(e) {
var app = UiApp.createApplication().setTitle("Upload CSV");
var formContent = app.createVerticalPanel();
formContent.add(app.createFileUpload().setName("dsrFile"));
formContent.add(app.createSubmitButton("Start Upload"));
var form = app.createFormPanel();
form.add(formContent);
app.add(form);
return app;
}
// Upload .CSV file
function doPost(e)
{
// data returned is a blob for FileUpload widget
var fileBlob = e.parameter.dsrFile;
var doc = DocsList.createFile(fileBlob);
}
e is undefined because you are not passing anything to doPost. You have to pass the needed object to doPost. Check where you call the function and what parameters do you pass to it if any. Even if you pass a parameter to that function, it holds undefined value. Make sure that you are passing the correct objects to your functions.
Your script should work perfectly. e is defined by Google Apps Script, not need to pass anything in particular is contains the fields of your form, in particular in this case the file you uploaded.
I would suspect you may be falling foul to the dev url vs publish url syndrome, where you are executing an old scrip rather that the code you are currently working on.
Be sure you script end with 'dev' and not 'exec'
https://script.google.com/a/macros/appsscripttesting.com/s/AKfyck...EY7qzA7m6hFCnyKqg/dev
Let me know if you are still getting the error after running it from the /dev url

How do I call an SSJS method with parameters from javascript

I have a url containing a hash e.g http://www.acme.com/home.xsp#key=1234
When the url above loads in the browser I need to call a serverside javascript based on the value in the hash.
I have found a few ways of retriving the hash client side like this
var key = getHashUrlVars()["key"];
so I have the key available in my client side script in the onclientload event.
So in the same onClientLoad event I now need to call my server side javascript method so I have tried the following
'#{javascript:doStuff(key)}'
'#{javascript:doStuff(' + key + ')}'
..and a few other ways. but I can't get it to work.
maybe there is an XSP command I can use instead?
any ideas how to solve this?
You could do a XSP.partialRefreshPost in CSJS and use parameters to send your data to the server:
var p = { "key": getHashUrlVars()["key"] }
XSP.partialRefreshPost( '#{id:_element_to_refresh_}', {params: p} );
To access the parameters in SSJS just try this:
doStuff( param.key )
You could use an empty div-element as a target execute the SSJS code. Or you can use the executeOnServer - method: http://xpages.info/XPagesHome.nsf/Entry.xsp?documentId=88065536729EA065852578CB0066ADEC
Hope this helps
Sven

Resources