Telegram bot - How to handle conversations? - python

I am trying to learn to make a telegram bot but I am not sure how to achieve continuous conversations. All I know is how to respond to the individual messages, for example like this -
If a user enters wrong command, for example /jnaddaad
def unknown_response(update: Update, context: CallbackContext):
update.message.reply_text(
"Sorry I can't recognize you , you said '%s'" % update.message.text)
My use-case is simple -
User enters his country using /addcountry command.
After the country, I will as ask what city is he from, and he should be able to answer using /addcity command.
After city, he should be able to enter the addresses(multiple) using /addaddresses
I save everything in a database - username, country, city and addresses.
User can update/delete one or more addresses.
Note:- User should not be directly able to enter city without country, and addresses with city. So the flow should be addcounty -> addcity -> addaddresses. And without the previous steps, user should not be able to access current steps.
I can probably be able to do 1 and 4. I just want a direction on how can I achieve the asked. Do I need to maintain a database with user and current user's username and steps they have performed till now, or is it possible with python-telegram-bot?

That should be achievable with just python-telegram-bot and the ConversationHandler.
The official docs for the ConversationHandler have a good explanation and also (more importantly) four fully working example bots.

Related

How can i get a list of all friends' names & birthdays from facebook

I need to get a list of all friends' names and birthdates from facebook to make a program to automatically send out birthday messages, but even after going through the facepy, and facebook documentation I couldn't find anything up to date that works.This is the closest i have gotten, this returns the amount of friends I have.
from facepy import GraphAPI
graph = GraphAPI('user_token')
query = graph.get("me")['name'] # user's name
print(query)
friend_count = graph.get("me/friends")['summary']['total_count'] # user's friend count
print(friend_count)
Getting all friends is not possible anymore, since many years. You can only get friends who authorized your App too, and they need to authorize with the correct permission.
Automatically sending messages would not be possible anyway for other reasons too, there is no API to automatically send messages from user to user, for example.
I found it
GET
https://graph.facebook.com/v13.0/me?fields=friends%7Bbirthday%7D&access_token=
it doesn't get information when in "self" mode

Finding a user by their username#discrim

Is it possible to get someone's user ID who you know their username and discrim, but is not in a mutual server with you?
Thanks.
I don't think so. It makes sense since this can easily be abused. Imagine all the spam bots if bots can see all discord users.
Below are two ways to get user info using discord.py, but note that they cannot be used as you ask.
client.get_user_info can be used to get user info even if you don't share a server, but it takes the unique ID as an argument.
server.get_member_named returns the unique user ID and takes username plus the optional discriminator as input, but requires that you share server/guild with the user to work.

Django Creating A Team Function

I want to design a simple app using Django. The design is as follows:
Each user has their own unique ID in the database called id which exists in the auth_user table already equipped with Django. Then I have a Team_ID which is another unique id that represents a team in table called team_profile. In this table I have the following columns: Member1, Member2, Member3. Currently, a user can create a team and this will set Member1 to the id if the creator.
Each user also has a profile page and in this profile page their is an invite button. This is where I am stuck. I am trying to write the invite function but I have absolutely no idea where to start. In the ideal world, I would like a notification to be sent to the invitee and the invitee can accept or decline the invitation. If the member accepted the invitation then Member2 will have this person's id. I am currently reading up on a lot of stuff but in the meantime if anyone of you guys have any suggestions that would be great.
There are multiple way to engineer your problem, I'm gonna suggest one, but eventually you may adapt it to your need ( i don't know your project in dept )
TeamMembership:
user1: user that send the request
user2: user that receive the request
status: here you can create a choice field where 1=pending, 2=accepted, 3=declined
The next thing you have to do is to manage the membership filtering based on the status.
This is a minimalistic approach that you can extend with for example the date of the invite etc.

Can facebook test users be verified?

It seems someone has asked this question before. However it is focus on the username, which I don't care about.
I have added a real email address to my test user and confirmed it by clicking on the verification email. However the test user is still marked as unverified.
This is a big problem when unit testing with test users.
graph = facebook.GraphAPI(oauth_access_token)
me = graph.get_object("me")
if 'verified' not in me or not me['verified']:
return False
I can't pass this line in my unit test, since test users are always unverified.
Any advice please?
https://developers.facebook.com/docs/graph-api/reference/v2.1/user:
Someone is considered verified if they take any of the following actions:
- Register for mobile
- Confirm their account via SMS
- Enter a valid credit card
… so simply confirming an email address does not even qualify as a “verified” criterion according to this.
If you do get an email back from the API, it should be confirmed by the user already. (Otherwise FB will not give it out in the first place.)
But you will not get an email address for every user. The user does not necessarily even have an email address on file with FB, so don’t rely on getting one to use in your app in the first place.

Adding to Google Group based on Google Spreadsheet data validation

So I'd like to preface this with the fact that I am NOT a programmer. I know a little Python, but that's about it.
So this is what I'm trying to do:
User fills out a Google Form and the responses are dumped into a spreadsheet. As a part of the form, their email address is captured in "'Form Responses'B:B"
Not sure if this is even possible, but I'd like to know:
for row X If column P = yes and column c = Accept and column F = Yes
Capture email from Column B and add them to GoogleGroup and send Welcome_Email
Please help! :)
Thanks in advance.
Google Apps Script is JavaScript.
When you build the form in Google Forms, set the Data Validation for the email field to require email address. That way you don't have to hassle with validating it yourself.
http://googledrive.blogspot.com/2013/09/four-new-ways-to-customize-your-google.html
Once the form is built, go to Tools, Script Editor, and make a new Blank Project.
Replace any code in there with this:
function addToGroup(e){
var itemResponses = e.response.getItemResponses();
var emailAddress = itemResponses[#].getResponse();
var group = GroupsManager.getGroup("group name").addMember(emailAddress);
GmailApp.sendEmail(emailaddress, "Welcome to Group Name!", "You have been successfully added to Group Name");
}
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String)
Replace # with the zero-indexed number of the column containing the email addresses, and group name is the mailbox name of the group on your Google Apps domain.
Then you just need to send the welcome email.
Save the project, and go to Resources> Current Script's Triggers. Set it run the addToGroup script on FormSubmit, Authorize it when prompted, and that should be it for you.
https://developers.google.com/apps-script/reference/domain/groups-manager
http://support.google.com/a/bin/answer.py?hl=en&answer=60757
This only works with Google Apps accounts, and only if you have the Provisioning API turned on in your domain admin console. If you're using a standard consumer Google account, they don't have anything available to do this.
Doing a few searches on this forum on words like email or groups you'll find quite a few scripts and if you spend a couple of hours looking at how they are built you'll be able to write something that does what you want...
If at that moment you meet some unexpected difficulties you'll be welcome to ask for help if you show what you've done.

Categories