dialogflow - how to get session id? - python

First of all, I'm a noob with dialogflow and web services. I'm trying to integrate a dialogflow agent I just created and integrate it with my app on my local computer. I was able to get project_id and all other important information but no matter where I look, no one seems to talk about where they get session ids from. Here is the audio-to-text code that I'm using that was forked from api.ai github page:
import os
import dialogflow_v2 as dialogflow
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "My Google Credential"
project_id = 'project id'
session_id = "this i don't know where to get"
audio_file_path = 'my wave file directory name'
language_code = 'en'
def detect_intent_audio(project_id, session_id, audio_file_path,
language_code):
"""Returns the result of detect intent with an audio file as input.
Using the same `session_id` between requests allows continuation
of the conversaion."""
session_client = dialogflow.SessionsClient()
# Note: hard coding audio_encoding and sample_rate_hertz for simplicity.
audio_encoding = dialogflow.enums.AudioEncoding.AUDIO_ENCODING_LINEAR_16
sample_rate_hertz = 44100
session = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session))
with open(audio_file_path, 'rb') as audio_file:
input_audio = audio_file.read()
audio_config = dialogflow.types.InputAudioConfig(
audio_encoding=audio_encoding, language_code=language_code,
sample_rate_hertz=sample_rate_hertz)
query_input = dialogflow.types.QueryInput(audio_config=audio_config)
response = session_client.detect_intent(
session=session, query_input=query_input,
input_audio=input_audio)
print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
print('Detected intent: {} (confidence: {})\n'.format(
response.query_result.intent.display_name,
response.query_result.intent_detection_confidence))
print('Fulfillment text: {}\n'.format(
response.query_result.fulfillment_text))
detect_intent_audio(project_id, session_id, audio_file_path,
language_code)
I enabled webhook and linked the webhook to heroku, but still I don't see where I can get this session ID. Can someone help me?

On the link,
https://dialogflow.com/docs/reference/api-v2/rest/v2/projects.agent.sessions/detectIntent
, it is stated under the HTTP request session path parameters that
"It's up to the API caller to choose an appropriate session ID. It can be a random number or some type of user identifier (preferably hashed). The length of the session ID must not exceed 36 bytes."

Related

Youtube Data API v3 My list of Subscriber is not complete

I'm trying to get a list of all my subscribers using Youtube Data Api. But a large amount of my public subscribers aren't appearing.
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(api_service_name, api_version, credentials=credentials)
curSubDicList = []
nextPageToken = ""
while TRUE: #Loop through all subs and fill out the curSubDicList var
request = youtube.subscriptions().list(part="subscriberSnippet", mySubscribers=True, maxResults=50, pageToken=nextPageToken, order="alphabetical")
response = request.execute()
for x in response["items"]:
print(x["subscriberSnippet"]["title"])
subDic = { "title" : x["subscriberSnippet"]["title"], "channelId" : x["subscriberSnippet"]["channelId"], "pfp" : x["subscriberSnippet"]["thumbnails"]["default"]["url"]}
curSubDicList.append(subDic)
if "nextPageToken" in response:
nextPageToken = response["nextPageToken"]
else:
break
I loop through using each new nextPageToken till there isn't one. But for some reason a lot of my public subscribers aren't appearing. I print out my subs to my file subs.json
jsonSubsTxt = open("subs.json", "w", encoding="utf-8")
curSubDicListJsonStr = json.dumps(curSubDicList)
jsonSubsTxt.write(curSubDicListJsonStr)
jsonSubsTxt.close()
I know that a lot of my public subs are missing by using Youtube Studio's built in subscriber viewer, and CTRL + Fing for them in the subs.json. I have a 2nd youtube account called "LowerLevelLemmy" I made sure that my subscriptions were public and subscribed to myself. I appear as a sub in YT Studio, but not in my subs.json. Here's images of what I mean:
My 2nd channel appears in YT Studio as a public sub.
But CTRL Fing for it shows no results.
Sometimes CTRL+Fing someone I see as a sub in YT Studio works but most times it doesn't. When their username doesn't come up with a match, I try searching with their channel ID, but that doesn't work either.
I'm new to Youtube Data Api and this is my first time submitting a question to Stack Over Flow. I hope I did everything right. Thank you for reading.

dialogflow - ImportError: cannot import name 'AgentsClient'

I'm a noob with dialogue flow. Here is the audio-to-text code that I'm using.
I googled the error but couldn't find a solution. Any help is appreciated. Thank you!
import os
import dialogflow_v2 as dialogflow
from dialogflow_v2 import AgentsClient
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"D:\Resume building\gcloudtstuff24Jan2020\testing-302710-cc7fef4033ff.json"
project_id = '*******'
session_id = "ayesha"
audio_file_path = r'C:\Users\ayesha\Downloads\sample - mp3 inserted.wav'
language_code = 'en'
def detect_intent_audio(project_id, session_id, audio_file_path, language_code):
"""Returns the result of detect intent with an audio file as input.
Using the same `session_id` between requests allows continuation
of the conversation."""
from google.cloud import dialogflow
session_client = dialogflow.SessionsClient()
# Note: hard coding audio_encoding and sample_rate_hertz for simplicity.
audio_encoding = dialogflow.AudioEncoding.AUDIO_ENCODING_LINEAR_16
sample_rate_hertz = 16000
session = session_client.session_path(project_id, session_id)
print("Session path: {}\n".format(session))
with open(audio_file_path, "rb") as audio_file:
input_audio = audio_file.read()
audio_config = dialogflow.InputAudioConfig(
audio_encoding=audio_encoding,
language_code=language_code,
sample_rate_hertz=sample_rate_hertz,
)
query_input = dialogflow.QueryInput(audio_config=audio_config)
request = dialogflow.DetectIntentRequest(
session=session,
query_input=query_input,
input_audio=input_audio,
)
response = session_client.detect_intent(request=request)
print("=" * 20)
print("Query text: {}".format(response.query_result.query_text))
print(
"Detected intent: {} (confidence: {})\n".format(
response.query_result.intent.display_name,
response.query_result.intent_detection_confidence,
)
)
print("Fulfillment text: {}\n".format(response.query_result.fulfillment_text))
detect_intent_audio(project_id, session_id, audio_file_path, language_code) dialogflow services.
And I am getting
ImportError: cannot import name 'AgentsClient'
I was creating my json file directly from the project I already have in my google cloud. I created a new project in the dialogue flow and it worked.

Spotify API Authorization to create a Playlist

I've been trying to write a code with the Spotify for Developers Tools that will read the audio features of all the songs in a user selected playlist and then create two new playlists to sort the songs in the original playlist depending on whether they are considered minor or major. I have gotten most of the program to work, it reads the playlist and creates a dictionary of the songs audio features and creates two lists for the songs that are minor and one for the major, however it will not let me create a new playlist due to the authorization and I can not figure out how to solve this issue. Below is my code, any help is greatly appreciated!
Here is what the beginning looks like with my account information:
################### Account Information ######################
cid ='*my client id*' # Client ID
secret = '*my secret id*' # Client Secret
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
username = '*my username*'
token = util.prompt_for_user_token(username=username, scope='playlist-modify-public', client_id=cid, client_secret=secret, redirect_uri="http://localhost:8888/callback")
#############################################################
Then I try to create the playlists:
############### Create Playlist #################
created_playlist_minor = sp.user_playlist_create(username, "New Playlist Minor", description='Minor')
created_playlist_major = sp.user_playlist_create(username, "New Playlist Major", description='Major')
created_playlist_other = sp.user_playlist_create(username, "New Playlist Other", description='Other')
#################################################
And then it gives me an error
spotipy.client.SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/users/*username*/playlists:
This request requires user authentication.
You are passing in a SpotifyClientCredentials object to your Spotify client. This authenticates spotipy.Spotify so that you can do anything that your developer API key gives you access to (e.g. getting info about songs in a public playlist). You haven't given your spotipy.Spotify a user's authentication token, so you can't do user-specific things like creating a playlist. You should do the following:
username = '*my username*'
token = util.prompt_for_user_token(
username=username,
scope='playlist-modify-public',
client_id=cid,
client_secret=secret,
redirect_uri="http://localhost:8888/callback"
)
sp = spotipy.Spotify(auth=token)
Note that you don't have to pass your Client ID & Secret to spotipy.Spotify, the token is sufficient

How can I use Python Keyring in conjunction with Box API OAuth2?

I'm pretty new to Python and programming and I am trying to figure out how to automate the box.com authentication process and it's kicking my butt. Any help would be appreciated!
I have this code below, which obviously wasn't mine but came from a tutorial. I am trying to figure out the
keyring.get_password('Box_Auth', 'mybox#box.com')
I'm thinking the mybox#box.com is my redirect URI? But I'm not sure what it is looking for when it asks for the Box_Auth.
Here is the full code
"""An example of Box authentication with external store"""
import keyring
from boxsdk import OAuth2
from boxsdk import Client
CLIENT_ID = ''
CLIENT_SECRET = ''
def read_tokens():
"""Reads authorisation tokens from keyring"""
# Use keyring to read the tokens
auth_token = keyring.get_password('Box_Auth', 'mybox#box.com')
refresh_token = keyring.get_password('Box_Refresh', 'mybox#box.com')
return auth_token, refresh_token
def store_tokens(access_token, refresh_token):
"""Callback function when Box SDK refreshes tokens"""
# Use keyring to store the tokens
keyring.set_password('Box_Auth', 'mybox#box.com', access_token)
keyring.set_password('Box_Refresh', 'mybox#box.com', refresh_token)
def main():
"""Authentication against Box Example"""
# Retrieve tokens from secure store
access_token, refresh_token = read_tokens()
# Set up authorisation using the tokens we've retrieved
oauth = OAuth2(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
access_token=access_token,
refresh_token=refresh_token,
store_tokens=store_tokens,
)
# Create the SDK client
client = Client(oauth)
# Get current user details and display
current_user = client.user(user_id='me').get()
print('Box User:', current_user.name)
Again, I would really appreciate any help!
I was having the exact same issue.
You will need an access token and a refresh token.
Read here how to generate those.

Tweepy + App Engine Example OAuth Help

Hi I am trying to follow the Tweepy App Engine OAuth Example app in my app but am running into trouble.
Here is a link to the tweepy example code: http://github.com/joshthecoder/tweepy-examples
Specifically look at: http://github.com/joshthecoder/tweepy-examples/blob/master/appengine/oauth_example/handlers.py
Here is the relevant snippet of my code [Ignore the spacing problems]:
try:
authurl = auth.get_authorization_url()
request_token = auth.request_token
db_user.token_key = request_token.key
db_user.token_secret = request_token.secret
db_user.put()
except tweepy.TweepError, e:
# Failed to get a request token
self.generate('error.html', {
'error': e,
})
return
self.generate('signup.html', {
'authurl': authurl,
'request_token': request_token,
'request_token.key': request_token.key,
'request_token.secret': request_token.secret,
})
As you can see my code is very similar to the example. However, when I compare the version of the request_token.key and request_token.secret that are rendered on my signup page
I.e. the variables I output to the browser:
request_token.key
request_token.secret
Are not the same as the data stored in the datastore:
db_user.token_key = request_token.key
db_user.token_secret = request_token.secret
db_user.put()
As an example here is what I am seeing when testing:
Printed to the screen:
request_token.key: MocXJxcqzDJu6E0yBeaC5sAMSkEoH9NxrwZDDvlVU
request_token.secret: C7EdohrWVor9Yjmr58jbObFmWj0GdBHMMMrIkU8Fds
Values in the datastore:
token_key: 4mZQc90GXCqcS6u1LuEe60wQN53A0fj7wdXHQrpDo
token_secret: dEgr8cvBg9jmPNhPV55gaCwYw5wcCdDZU4PUrMPVqk
Any guidance on what I am doing wrong here?
Thanks!
Reference Links:
Here is a sample code to get Twitter followers-count for a single user using Tweepy (version 2.0) on Google App Engine (GAE) in Python (version 2.7).
# ----GAE MODULES-----------
import webapp2
from webapp2_extras import jinja2
from google.appengine.api import users
import tweepy
import urlparse
import logging
# ----JINJA2 TEMPLATE----------
class TemplateHandler(webapp2.RequestHandler):
#webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def render_template(self, filename, **template_args):
logging.info('calling jinja2 render function %s %s', self, filename)
self.response.write(self.jinja2.render_template(filename, **template_args))
# ----CODE--------------------
class TwitterTweepyImplementation(TemplateHandler):
'''
All Tweepy related methods are handled in this class
'''
#All methods that expect HTTP GET
twitter_tweepy_impl_get_methods = {
'/tweepyimpl/oauthRedirect': 'redirect_to_twitter_for_user_to_enter_uname_and_pwd',
'/tweepyimpl/oauthCallback': 'handle_callback_from_twitter_after_user_authentication',
}
def get(self):
'''
All twitter specific get actions are handled here
'''
#identify page to display from the path in the URL
rcvd_url = self.request.path
#to keep the code a little easier to understand, there are no security checks or exception handling coded added in
#this code example, so please add those on your own.
#get destination method using key-value pair
dest_method = self.__class__.twitter_tweepy_impl_get_methods.get(rcvd_url, None)
if dest_method:
func = getattr(self, dest_method, None)
if func:
func()
return
def redirect_to_twitter_for_user_to_enter_uname_and_pwd(self):
"""
Twitter OAuth Redirection: redirects user to Twitter for entering user name and password
"""
logging.info('redirect_to_twitter_for_user_to_enter_uname_and_pwd')
auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, YOUR_OWN_REDIRECT_URL_AFTER_TWITTER_AUTHENTICATION)
'''YOUR_OWN_REDIRECT_URL_AFTER_TWITTER_AUTHENTICATION: you can set this everytime above or once at twitter.com from where
you get your Consumer Key and Consumer Secret. E.g., http://www.yourwebsite.com/tweepyimpl/oauthCallback'''
#get Twitter redirect url where user enters credentials (uname and pwd)
auth_url = auth.get_authorization_url(); #logging.info("auth_url = %s", auth_url);
#store temp credentials as browser cookies (these need to be stored in the browser so that after user completes authentication
#at Twitter.com, when user is redirected to the return URL above by Twitter (= YOUR_OWN_REDIRECT_URL_AFTER_TWITTER_AUTHENTICATION)
#your application server knows for which user this redirect is for).
self.response.set_cookie('token_key', auth.request_token.key)
self.response.set_cookie('token_secret', auth.request_token.secret)
#redirect user's browser to twitter auth URL where user can enter username and pwd
self.redirect(auth_url)
return
def handle_callback_from_twitter_after_user_authentication(self):
"""
Callback from Twitter after user enters user name and pwd at twitter.com URL
"""
logging.info('handle_callback_from_twitter_after_user_authentication')
#Twitter redirected browser here. Now read verifier and determine if user twitter authentication succeeded, failed, or was
#canceled by the user
auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
verifier = self.request.get('oauth_verifier', None); #logging.info('verifier = %s', verifier)
#user canceled twitter oauth
if not verifier:
self.redirect('your_app_url') #add your own url here.
return
#fetch temp credentials from browser cookies (as set during redirect_to_twitter_for_user_to_enter_uname_and_pwd method).
token_key = self.request.cookies['token_key'];
token_secret = self.request.cookies['token_secret'];
#now exchange temp credentials for user specific access token
auth.set_request_token(token_key, token_secret)
#parse access token string to extract the key and the secret
access_token = auth.get_access_token(verifier=verifier); logging.info('access_token = %s', access_token)
params = urlparse.parse_qs(str(access_token), keep_blank_values=False)
access_key = params['oauth_token'][0]; logging.info('access_key = %s', access_key)
access_secret = params['oauth_token_secret'][0]; logging.info('access_secret = %s', access_secret)
#add access token information to the datastore for periodic fetch of Twitter information later on for this user, e.g., via a cron job.
user_obj = UserTwitterAccessTokenStorageDatabase.get_by_key_name(users.get_current_user().email())
user_obj.access_key = access_key
user_obj.access_secret = access_secret
user_obj.put()
auth.set_access_token(access_key, access_secret) #this statement you can use later on to fetch twitter data for any user whose
#access-key/secret you have stored in your database. For example, via a cron job.
#User does NOT need to be visiting your website for you to fetch twitter data for the user.
#use tweepy api now to get user data from Twitter
api = tweepy.API(auth)
me = api.me()
#display debug information
logging.info("me = %s", me)
logging.info('me.id_str = %s, name = %s, screen_name = %s', me.id_str, me.name, me.screen_name)
#get followers count for this user
user = api.get_user(me.id_str)
logging.info('num_followers = %s', user.followers_count)
#you have the required information - in this code example followers-count. now redirect user to your app determined URL
self.redirect('your_app_url') #add your own url here.
app = webapp2.WSGIApplication([
('/tweepyimpl/.*', TwitterTweepyImplementation)
], debug=const.DEBUG)
It seems you use twice request_token, request_token.key and request_token.secret. The second time ( in self.generate) you should read their values from your database and not request them again.

Categories