How do I check what the admin status is in my telegram bot? - telegram-bot

I need to write some logic in my bot which checks if a user is admin/owner/creator/member etc
I can see when I look at the user data structure, it has a status attribute with, in the case below, the value of 'member'. How can I check for admin, owner and creator?
Array
(
[user] => Array
(
[id] => 123456789
[is_bot] =>
[first_name] => John
[username] => Doe
[language_code] => en
)
[status] => member
)

According to the Botman User Extension class:
The member's status in the chat.
Can be “creator”, “administrator”, “member”, “restricted”, “left” or “kicked”.

Related

workfront API updating Users task time in their time sheet

I want to update a tasks work hours and have it also update that task on the users assigned time sheet.
I get the user's time sheet id and I call https://*********.attask-ondemand.com/attask/api/v4.0/hour?
Posting
[taskID] => 55e06e67001af6e*********
[timesheetID] => 55d0283a0022************
[entryDate] => 2015-08-28
[hours] => 3
[apiKey] => *****************
This successfully updates the the task actual hours but it does not update the task field in the users time sheet.
However if I get the user time sheet for the current period
data] => Array
(
[0] => Array
(
[ID] => 55d0283a00223e********
[displayName] => ********** 8/23/15 - 8/29/15
[objCode] => TSHET
[approverID] => 551b350d************************
[customerID] => 52fe8***************************
[endDate] => 2015-08-29
[extRefID] =>
[hasNotes] =>
[hoursDuration] => 48
[lastNoteID] =>
[lastUpdateDate] => 2015-08-15T23:05:46:727-0700
[lastUpdatedByID] =>
[overtimeHours] => 0
[regularHours] => 10
[startDate] => 2015-08-23
[status] => O
[timesheetProfileID] => 54ed0********************
[totalHours] => 10
[userID] => 551f********************
)
)
The total hours is updated, but the timesheet in the web gui does not reflect that anywhere.
I need to update the task and the user time sheet with hours worked on an assigned task.
thanks
You may need to add the hourTypeID field in the post. This works for v9 at least.
The hourType is used in WF/AtTask for determining whether an hour is a project hour, a task hour, or an overhead hour (which in itself has different types -- some of them custom to a particular organization). You can see what kinds of hourTypes you have here:
https://*********.my.workfront.com/attask/api/v9.0/hourType/search?isActive=true
I usually filter the query further, but I hope this is a good starting point for anyone reading.
I know this was posted a while back but I was working with updating timesheets over the past week and thought this may be helpful.

select instance where instance OR associations match fuzzy text

This is hard to explain, so I'll start with what I've got.
User has_many Dependents
Users and Dependents have names.
I want a scope on User that will return all of the users whose names, OR whose dependents names match a certain fuzzy text.
It seems to not return all of the expected results. This is my current scope:
scope :by_fuzzy_text, lambda { |text|
text = "%#{text}%"
joins(:dependents).where('name ILIKE ? OR dependents.name ILIKE ?', text, text).uniq
}
The issue is that it's returning unexpected results. It seems to only work on the first 11 Users in my database, despite there being over 100. So no matter how perfectly the scope matches, it will only return the results who are one of the first 11 users in the database. I do NOT have this issue if I'm not using Joins.
Hopefully that makes sense. The uniq on the end is because if the user has multiple dependents, the user is returned multiple times, once for each dependent.
Example:
User1 = "Sam Smith"
-Dependent1.1 = "Ralph Smith"
-Dependent1.2 = "Alex Smith"
User2 = "April Shower"
-Dependent2.1 = "Zach Shower"
-Dependen2.2 = "Sally Smith"
User.by_fuzzy_text('w') => [User2]
(April Shower, Zach Shower)
User.by_fuzzy_text('z') => [User2]
(Zach Shower)
User.by_fuzzy_text('x') => [User1]
(Alex Smith)
User.by_fuzzy_text('smith') => [User1, User2]
(Sam Smith, Ralph Smith, Alex Smith, Sally Smith)
I needed to do a LEFT OUTER JOINS to include the Users who did not have any dependents.
joins('LEFT OUTER JOIN dependents ON users.id = dependents.user_id').where('name ILIKE ? OR name ILIKE ?', text, text).uniq`

Select conversations of user but not the user

I have a database like
User
UserName
UserId
Chat
ChatId
ChatSubject
ChatUser
Chat_ChatId
User_UserId
ChatMessage
Chat_ChatId
User_UserId
Message
I have the current user id in a variable like : $userId
What i want is find the Chat_ChatSubject an the UserName of the other person in the chat, so i can create a list of conversations where i show the username of the other person and the subject
What i have :
$conversations= Chat::
Join('ChatUser', 'Chat.id', '=', 'ChatUser.User_UserId')
->Join('User', 'User.UserId', '=', 'ChatUser.User_UserId')
->Select('Users.UserName', 'Chat.Subject')
->OrderBy('ChatUsers.Chat_ChatId','asc')
->GroupBy('Chat.ChatId')
->get();
The result of this is a list of Chats but it contains not always the name of the other person. What i want is that User.UserName is the username of the other person so not my own.
(There are always 2 ChatUsers in one Chat
Assuming you have the relations set up in your models...
$conversations = User::find($userId)
->chats()
->with(array('users' => function($query) use ($userId){
$query->where('id', '!=', $id);
}))->get();
foreach($conversations as $conversation){
$conversation->ChatSubject; // get subject
$conversation->users; // get users collection (current user excluded)
}

Primary Category ID for Venue in /v2/venues

With the new version of the Foursquare API - I'm finding it difficult to identify the PRIMARY category ID from a /v2/venue lookup. For eaxmple, in the listing below, there is no way to tie the Bowlling Alley to Arts & Entertainment. My fear is that foursquare will introduce new categories and not tell us to which primary category it is being linked to. Any suggestions for this?
Note: I know that I can tell a pull of the categories from /v2/venue/categories and store the IDs of the primary and do a new lookup. Again - my fear is that foursquare introduces a new category and with the primary - it's hard to do the lookup.
[categories] => Array
(
[0] => stdClass Object
(
[id] => 4bf58dd8d48988d1e4931735
[name] => Bowling Alley
[pluralName] => Bowling Alleys
[shortName] => Bowling Alley
[icon] => stdClass Object
(
[prefix] => https://foursquare.com/img/categories/arts_entertainment/bowling_
[sizes] => Array
(
[0] => 32
[1] => 44
[2] => 64
[3] => 88
[4] => 256
)
[name] => .png
)
[primary] => 1
)
)
For now you'll just have to refresh the categories mapping every once in a while to assure yourself that it's fresh. Agreed that not having the parent category in the category object is annoying -- checking if there's a reason why it's not there already.

Returning array of objects via named_scope -- has_many...belongs_to association; UNION ALL query

I'm looking for an answer that will return an array of user objects via (preferably) a named_scope or via a class method on the User model that does some manipulation.
So without further ado...
I have two tables: users and fights.
User has many fights (has_many :fights, :foreign_key => 'challenger_id or challengee_id')
Fight belongs to user (belongs_to :challenger, :class_name => 'User'...belongs_to :challengee, :class_name => 'User')
Fight has the following columns of concern:
challenger_id (user_id fk)
challengee_id (user_id fk)
challenger_won (boolean)
As you can see, a user can be a challenger or a challengee, but not both.
If the user is a challenger and the challenger_won = true, then it is considered a win.
If the user is a challengee and the challenger_won = false, then it is considered a win.
If the challenger_won = null, then just disregard it.
I have a raw SQL statement that returns a fighter attribute (the user_id) grouped by most wins attribute:
SELECT a.fighter, COUNT(*) AS wins
FROM (SELECT challenger_id AS fighter
FROM fights
WHERE challenger_won = TRUE
UNION ALL
SELECT challengee_id AS fighter
FROM fights
WHERE challenger_won = FALSE
) AS a
GROUP BY a.fighter;
So given this info, how can I return an array of user objects via (preferably) a named_scope or via a class method on the User model that does some manipulation?
I think you can try something like this to recreate the result of you query:
class User
named_scope :winners,
:select => 'users.*, COUNT(fight.id) AS wins',
:join => :fights,
:conditions => ['(fights.challenger_id = user_id AND fights.challenger_won = TRUE) OR (fights.challengee_id = user_id AND NOT fights.challenger_won = FALSE)']
:group => 'user_id'
end
However, for caching puposes, you might want to consider adding a win_count field to the User model. If you create a setter method for the winner of a fight, you can increment the win_count right at that moment when it changes.
Or, without the join (mysql specific)
class User
named_scope :winners,
:select => 'if(challenger_won = FALSE,challenger,challengee) as winner,COUNT(id) AS wins ',
:group => 'user_id'
end