Automated Customer Support Using AI and Social Media
- Integrate the Twitter API with Google Cloud AI to automatically respond to customer inquiries and complaints posted on Twitter in real-time.
- Utilize Google Cloud Dialogflow to build intelligent chat agents that can understand natural language queries and provide automated responses.
- Use Google Cloud Language API to analyze the sentiment of tweets to prioritize responses or escalate to human agents for negative sentiments.
import os
from google.cloud import language_v1
from google.cloud import dialogflow_v2 as dialogflow
import tweepy
def twitter_auth():
# Twitter API credentials
consumer_key = 'YOUR_CONSUMER_KEY'
consumer_secret = 'YOUR_CONSUMER_SECRET'
access_token = 'YOUR_ACCESS_TOKEN'
access_token_secret = 'YOUR_ACCESS_TOKEN_SECRET'
# Authenticate with Twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return tweepy.API(auth)
def analyze_sentiment(text_content):
# Initialize the Google Cloud Natural Language API client
client = language_v1.LanguageServiceClient()
# Document configuration
type_ = language_v1.Document.Type.PLAIN_TEXT
language = "en"
document = {"content": text_content, "type_": type_, "language": language}
# Sentiment analysis
response = client.analyze_sentiment(request={"document": document})
sentiment = response.document_sentiment
return sentiment.score
def dialogflow_response(project_id, session_id, text_input):
# Initializes the Dialogflow client
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
# Configuring the text input
text_input = dialogflow.types.TextInput(text=text_input, language_code="en")
query_input = dialogflow.types.QueryInput(text=text_input)
# Get the response from Dialogflow
response = session_client.detect_intent(request={"session": session, "query_input": query_input})
return response.query_result.fulfillment_text
def respond_to_tweets():
# Twitter API instance
api = twitter_auth()
# Fetch the latest tweets containing your company's handle
tweets = api.search(q="@YOUR_COMPANY_HANDLE", count=50, lang="en", tweet_mode="extended")
project_id = "YOUR_DIALOGFLOW_PROJECT_ID"
session_id = "session_id"
for tweet in tweets:
user = tweet.user.screen_name
tweet_text = tweet.full_text
sentiment_score = analyze_sentiment(tweet_text)
if sentiment_score < 0:
# Escalate or prioritize for customer support
print(f"Escalate to human agent: {tweet_text}")
else:
response_text = dialogflow_response(project_id, session_id, tweet_text)
# Respond using Twitter API
api.update_status(status=f"@{user} {response_text}", in_reply_to_status_id=tweet.id)
if __name__ == "__main__":
respond_to_tweets()
Advantages and Outcomes
- Improve customer satisfaction by providing quick and accurate responses to inquiries directly through Twitter.
- Streamline workflow by automating common queries and letting human agents focus on more complex issues.
- Enhance brand presence on social media by actively interacting and resolving customer issues in real-time, fostering a positive brand image.