How to make an asynchronous http call from bot-composer? - azure

I'm creating a bot in botcomposer(v1.4.0) which involves an http request. What I'm looking for is an asynchronous call so that I don't have to wait for the response from the http call, while the user can continue to chat with the bot.
Also, I want to prompt the user when the response comes.
Thank you in advance.

Proactive messages would be the best option. Unfortunately, they are not implemented in Composer at this time. You might be able to create a custom action and accomplish your goal (my assumption without knowing more of what your requirements are).

Related

How to use the Discord JS Client without logging in?

I have a service that listens for certain updates and then uses a Discord Bot to post a message on a channel. A random example: every time a team scores a goal, post a message in a channel. I got an email from Discord to reset my bot token because there were too many logins.
As you know, in order to use the Bot you use the client.login("XXX") method. So every time there is an update my service initializes the bot and then sends a message. My assumption is that it has to do with me calling client.login("XXX") for every update. Since my service runs serverless, I can not keep the service up.
Is there a way to make the bot post a message without having to use client.login("XXX")? Or can anyone suggest any strategy to make this logic work?
That is not possible as a bot cannot do any actions without being logged in, it sounds like channel webhooks would fit your needs better.
You simply generate a webhook link in a channel which you can then post messages to through a post request.
You can read more about webhooks here
And you can read more about the required paramters and how to format a POST request to the webhook here

How to redirect react app to payment success page after completing payment by scanning a QR code?

I have a PERN(Postgres, Express, React, Node) e-commerce app. I want to notify a user that the payment is successful by redirecting to a payment successful page after a user scans QR code to pay.
However, I'm not sure how do I achieve this.
Currently, after the user completed a payment, I will receive a REST's POST request for payment confirmation from a bank API. . However, I'm also using GraphQL and I think that Graphql's Subscription could be what I'm trying to achieve.
Does anyone know how am I supposed to achieve this?
Are there anyways to send a request from REST to Graphql on my own server?
Or are there any better ways to do this?
Just like what is done in this video:
Do I understand correctly that you are looking for a way to notify your client that the payment was successful?
I assume you know how to handle redirect logic so here are the ways you can notify your web-app from the server.
1. Regular HTTP request
The client sends a regular HTTP-request to your server and you withhold the response until you received the 'payment completed' request from the bank API. If your request times out before the bank API confirms the payment - you simply send the same request again. This pattern is also known as Long Polling
2. Server-Sent Events
Your client can open a channel to your server that allow your server to send Events to your client (one-way). It is fairly easy to implement and a solid way to handle one-way communication. Check out the MDN documentation.
3. Web-Sockets MDN
You are probably familiar with these since they are the de-facto standard nowadays. Similar to Server-Sent Events but they allow two-way communication.
Most libraries that handle client-server communication implement a combination of these technologies in order to provide a fallback solution. I would recommend to stick to one of these libraries as they handle many edge-cases that are otherwise work-intensive to cover.
As you pointed out, if you are already using GraphQL you can simply use its subscriptions api. It uses WebSockets with a fallback to long-polling underneath.
Other popular options include socket.io or graphql-sse.

Callback for user's responses in a telegram bot (Telegraf)

I'm coding a telegram bot with Telegraf on node.js and I need to get some information about users. For example, the bot is asking them where they are from. Then, I want to wait for the user's response and store it in my db. However, I don't understand how to achieve that since I can't just use a bot.on('text', ...) because I'm asking multiple questions and need to differenciate responses afterwards. I found out that there are callbacks but all that I found is for buttons and keyboards. Thanks!
Oh, I found the solution, you need to use scenes to achieve that
https://github.com/telegraf/telegraf/issues/705

Caching response for API.ai Node.js webhook

I have a webhook designed in Node.js for API.ai that interacts with multiple API's to gather information and give response to user.
Since, I am interacting with multiple API's the response time taken is more than 5 secs which is causing the API.ai request to timeout.
To overcome this, I am trying to implement caching into the node.js webhook which saves the response from API's until a certain amount of time. This will remove timeout until the max-age header time is reached.
Edit: What is the best node module that I can use to cache the API responses for subsequest requests.
Note: I am using request node module for http requests but it doesnt seem to provide a way to cache the response.
All of the answers given are reasonable for tackling the cache problem on the request side. But since you specified API.AI and Actions, you might also be able to, or need to, store information while the conversation is in progress. You can do this using an API.AI context.
It may even be that if you limit it to just one remote call for each response from the user, you might be able to fit it in the timeframe.
For example, if you were having a conversation about movie times and ticket ordering, the conversation may go something like:
User: "I want to see a movie."
[You use an API to lookup the nearest theater, store the theater's location in a context and reply] "Your nearest theater is the Mall Megaplex. Are you interested in one there?"
User: "Sure"
[You now already have the theater, so you query for what it is playing with another API call and store it in a context] "There are seven different movies playing, including Star Wars and Jaws. Do those sound interesting?"
User: "No"
[You already have the data in the context, so you don't need another call.] "How about Rocky or..."
In this way you're making the same number of calls (generally), but storing the user's results in the session as you go as opposed to collecting all the information for the user, or all the possible results, and then narrowing them.
Finally decided to use the below module:
https://www.npmjs.com/package/memory-cache
This served my scenario better. Might try using Redis soon when i get some time.

Telegram different between getUpdates and Webhooks

id like to start using telegram bot, i already read the documentation, but still can't get my head, what's the difference between "getUpdates" and "webhooks"
source: telegram docs
can someone explain this in plain language
thanks in advance
The difference between "getupdates" and "webhook" is just like the difference between pull and push!
Using "getupdates" you don't even need to have a sever! You call telegram server prodically by providing bot's token and it will send you new updates if there is any. It means that your bot is always busy calling telegram even if there is just a single update per your 1000 requests!!
Using "webhook", you first notify telegram about your server ip and listening port and your public key. Then telegram will call your sever whenever there is any update.
At last and not the least If you want your bot to be faster you should use webhook.
Two method can get same content, but you can only use one of them at same time.
Webhook is dependent on HTTPS server, usually use in PHP.
If you haven't a web interface, better to choose getUpdates, which doesn't required HTTPS address to receive updates.

Resources