How to request all chats/groups of the user through Telegram Database Library(TDLib) for Node.js - node.js

The official example from telegram explains that in order to use getChats() command, one needs to set two parameters 'offset_order' and 'offset_chat_id'.
I'm using this node.js wrapper for the TDLib.
So when I use getChats() with the following params:
'offset_order': '9223372036854775807',
'offset_chat_id': 0,
'limit': 100
just like it is explained in the official docs:
For example, to get a list of chats from the beginning, the
offset_order should be equal to 2^63 - 1
as a result I get 100 chats from the top of the user's list.
What I can't understand is how do I iterate through that list? How do I use the API pagination?
When I try to enter a legitimate chat_id from the middle of the first 100, I still get the same first 100, so it seems like it makes no difference.
If I change that offset_order to ANY other number, I get an empty list of chats in return...
Completely lost here, as every single example I found says the same thing as the official docs, ie how to get the first 100.

Had the same problem, have tried different approaches and re-read documentation for a long time, and here is a decision:
do getChats as you do with '9223372036854775807' offset_order parameter
do getChat request with id of the last chat you have got. It's an offline request, just to tdlib.
here you get a chat object with positions property - get a position from here, looks like this:
positions: [
{
_: 'chatPosition',
list: [Object],
order: '6910658003385450706',
is_pinned: false
}
],
next request - getChats - use the positions[0].order from (3) as offset_order
goto (2) if there are more chats
It wasn't easy to come to this, so would be glad if it helps anybody who came from google like me :)

Related

How can I send an object as a query and use the $all operator in MERN stack?

I'm trying to make a filtering system for my web app using MERN stack. Currently I'm able to take in the user's requests for what to filter, take in that data, see which ones the user entered (because the user can also specify "Any" in a specific category, which means it would send an empty string or array to the backend), parse that data and turn it into one final object called request which would look something like this if a user decided to select SDG 8: Decent Work & Economic Growth as the SDG, Any as the assignment type, and Economy, People as the themes:
{
sdg: 'SDG 8: Decent Work & Economic Growth',
theme: [ 'Economy', 'People' ]
}
As you can see, the assignment_type value is not there because my backend code sees that they chose Any as the value and it returned an empty string which means I don't add it to my final request object.
So now that I have this object, I want to be able to send it to the database and return the objects that contain all of this information. I want to use the $all query statement for this because I need to make sure that if a user enters 'Economy', 'People' as the themes, an object that contains the themes 'Economy', 'People', 'Technology' also gets returned because the themes the user entered exists within that.
This is what I've tried so far in my test code but it doesn't seem to be working:
var request = {
sdg: 'SDG 8: Decent Work & Economic Growth',
theme: [ 'Economy', 'People' ]
}
const test = await Project.find( {$all: {request}}).sort({ createdAt: -1 })
console.log(test)
The code above just returns all values in my database and not the one's I've tried specifying. The main reason why I'm doing it this way is because I have to filter through the requests the user made in the backend in terms of which ones the user wanted specific values for, the user could specify "Any" for all 3 categories (sdg, theme, assignment_type) which would mean that the request object would just be empty so essentially I'd just be finding every single object in the database which is exactly what I want.
I'm not sure how I could make it so that I send these requests to the backend so it only finds those objects. I would appreciate some help

Let Alexa ask the user a follow up question (NodeJS)

Background
I have an Intent that fetches some Data from an API. This data contains an array and I am iterating over the first 10 entries of said array and read the results back to the user. However the Array is almost always bigger than 10 entries. I am using Lambda for my backend and NodeJS as my language.
Note that I am just starting out on Alexa and this is my first skill.
What I want to archive is the following
When the user triggers the intent and the first 10 entries have been read to the user Alexa should ask "Do you want to hear the next 10 entries?" or something similar. The user should be able to reply with either yes or no. Then it should read the next entries aka. access the array again.
I am struggling with the Alexa implementation of this dialog.
What I have tried so far: I've stumbled across this post here, however I couldn't get it to work and I didn't find any other examples.
Any help or further pointers are appreciated.
That tutorial gets the concept right, but glosses over a few things.
1: Add the yes and no intents to your model. They're "built in" intents, but you have to add them to the model (and rebuild it).
2: Add your new intent handlers to the list in the .addRequestHandlers(...) function call near the bottom of the base skill template. This is often forgotten and is not mentioned in the tutorial.
3: Use const sessionAttributes = handlerInput.attributesManager.getSessionAttributes(); to get your stored session attributes object and assign it to a variable. Make changes to that object's properties, then save it with handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
You can add any valid property name and the values can be a string, number, boolean, or object literal.
So assume your launch handler greets the customer and immediately reads the first 10 items, then asks if they'd like to hear 10 more. You might store sessionAttributes.num_heard = 10.
Both the YesIntent and LaunchIntent handlers should simply pass a num_heard value to a function that retrieves the next 10 items and feeds it back as a string for Alexa to speak.
You just increment sessionAttributes.num_heard by 10 each time that yes intent runs and then save it with handlerInput.attributesManager.setSessionAttributes(sessionAttributes).
What you need to do is something called "Paging".
Let's imagine that you have a stock of data. each page contains 10 entries.
page 1: 1-10, page 2: 11-20, page 3: 21-30 and so on.
When you fetching your data from DB you can set your limitations, In SQL it's implemented with LIMIT ,. But how you get those values based on the page index?
Well, a simple calculation can help you:
let page = 1 //Your identifier or page index. Managed by your client frontend.
let chunk = 10
let _start = page * chunk - (chunk - 1)
let _end = start + (chunk - 1)
Hope this helped you :)

How to get a certain (e.g. second or third) mention sent in a message

I've been trying to make a ship command that can either ship the author of the message with a mentioned user, or ship two mentioned users. I can get the 1st mention in a message but I have no idea on how to get the 2nd or even third mention in a message. I tried using:
message.mentions.users.first(2)
splitting the args then slicing them so only the second mention is avalaible, but this gives an "undefined" error when I try to get the username.
Could someone give me a script on exactly how to do it since I can't really get the hang of this
According to the documentation message.mentions.users yields a Collection. So you can just iterate over this collection or convert it to an array and then access the required index:
const userArray = message.mentions.users.array();
console.log(userArray[yourDesiredIndex]);

Duplicate mongodb objects params but only one gets shown? Last digits get rounded?

So I am experiencing a number of oddities with my db. It started when I noticed after I logged in that my balance was not what it used to be. Then I found two apparently different accounts that have the same number. Anyways. I am working with passport and the strategy is steam. Here is a look at my two issues in coding form:
First oddity:
*When using steam as the strategy I get back a user with a steam id.
{ steamid: '76561198053118469',
communityvisibilitystate: 3,
profilestate: 1,
personaname: 'What Comes Aroundâ„¢',
commentpermission: 1,
profileurl: 'https://steamcommunity.com/id/WCAOfficial/',
avatar:
'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/99/99f12061912d535322c9a23ee7d17ce341c27c56.jpg',
avatarmedium:
'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/99/99f12061912d535322c9a23ee7d17ce341c27c56_medium.jpg',
avatarfull:
'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/99/99f12061912d535322c9a23ee7d17ce341c27c56_full.jpg',
lastlogoff: 1587637203,
personastate: 1,
primaryclanid: '103582791466140922',
timecreated: 1321956195,
personastateflags: 0 }
This is the returned req.user. As you can see the user.steamid is: 76561198053118469
Here is my current users collection in my db:
[ { balance: 100,
_id: 5e9a43519eabc40017ed6a85,
userid: 76561198053118460,
profileurl: 'https://steamcommunity.com/id/WCAOfficial/',
__v: 0 },
{ balance: 0,
_id: 5ea22ae86407a43c389210bd,
userid: 76561198053118460,
profileurl: 'https://steamcommunity.com/id/WCAOfficial/',
__v: 0 } ]
So the first thing I don't understand is why the steam id has had it's last digit rounded. Now I didn't include an actual user in here, but one of my users has an id ending with something like 69, and that gets rounded to 80 for some reason. I cannot for the life of my understand this.
Next issue, ok so forget about the first issue. Fine, somewhere down the line the values get rounded or something. FINE. So if I use find( {userid: 76561198053118460}) I should get back both users with the two different balances, right? Well no, I don't get that back. Instead I get back the one that I get logged into, the one with 0 balance. This. This boggles my mind honestly. How this is possible doesn't make sense to me.
So there are my two issues. I'd like to end this question with a disclaimer. I have played around with mongoose for some time now I'd consider myself a beginner. So I really hope it's a problem with an answer is that real obvious but if it is, sorry for the noob question.
Thanks!
If you open your favorite browser's console and type 76561198053118469 in there, you are going to get back 76561198053118460. This is because 76561198053118469 is interpreted as a floating-point number (in javascript) and thus is stored inexactly.
Make sure everywhere you are operating on user ids you are using strings and are storing the ids as strings, not numbers.

Can i put Many Pictures links in dialogflow and send only one Randomly

I am new to dialogflow. I learned some things the previous month, but I am still searching for how to put a lot of pictures in intent and sending only one to the user, not all of them, and randomly, such as text responses as a kind of entertainment inside the Agent ...
Note :
I linked the client to Facebook Messenger and I want to send the photo there
Can i do this?
Assuming you're using Dialogflow-fulfillment library to respond to webhook requests this should do the trick.
You can store whatever type of response that you want to send in an array...
For example, I'll choose an array of plain text responses
const responses = [
"I didn't get that. Can you say it again?",
'I missed what you said. What was that?',
'Sorry, could you say that again?',
'Sorry, can you say that again?',
'Can you say that again?',
"Sorry, I didn't get that. Can you rephrase?",
'Sorry, what was that?',
'One more time?',
'What was that?',
'Say that one more time?',
"I didn't get that. Can you repeat?",
'I missed that, say that again?'
]
Given the array you can generate a random number between 0 (most programming languages index from 0) and the length of the array.
const index = Math.floor((Math.random() * responses.length) + 1)
In the above case, index is assigned to any number between 0 and 11. You can then pass this index into the array to randomly choose 1 value.
agent.add(responses[index])
You can take this concept and apply it to any response type you want.
Hope this does the trick for you!

Resources