如何借助Google Assistant调用外部API(如Jenkins API)?是否用google-assistant-sdk+Python开发?
Hey Sunny, great question—integrating Google Assistant with external APIs like Jenkins is such a handy way to automate devops workflows, and I’ve helped a few folks work through this exact setup. Let’s break this down step by step, starting with the best practices for calling external APIs, then tackling whether Python + the Google Assistant SDK is the right call for your app.
The key here is to create a clean, secure bridge between Google Assistant’s voice commands and your target API. Here’s the most reliable workflow:
1. Build a lightweight middleware layer (highly recommended)
Google Assistant’s interaction model doesn’t always play nicely with raw external APIs (like Jenkins’ auth requirements or parameter formats). A middleware layer acts as a translator: it takes requests from Google Assistant, formats them to match the external API’s specs, handles authentication, and sends back a user-friendly response.
For Python devs, Flask or FastAPI are perfect for this. Here’s a quick example of a Flask endpoint that triggers a Jenkins build:
from flask import Flask, request, jsonify import requests from requests.auth import HTTPBasicAuth app = Flask(__name__) # Jenkins config (store these in environment variables in production!) JENKINS_BASE_URL = "https://your-jenkins-instance.com" JENKINS_USER = "your-jenkins-username" JENKINS_API_TOKEN = "your-jenkins-api-token" @app.route('/jenkins/trigger-build', methods=['POST']) def trigger_jenkins_build(): # Extract job name from Google Assistant's request req_data = request.json job_name = req_data.get('job_name') if not job_name: return jsonify({"error": "Please specify a job name"}), 400 # Call Jenkins API to trigger build jenkins_build_url = f"{JENKINS_BASE_URL}/job/{job_name}/build" response = requests.post( jenkins_build_url, auth=HTTPBasicAuth(JENKINS_USER, JENKINS_API_TOKEN) ) if response.status_code == 201: return jsonify({"message": f"Successfully triggered build for {job_name}!"}) else: return jsonify({"error": f"Failed to trigger build: {response.text}"}), response.status_code if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, ssl_context='adhoc') # Use proper SSL in production
2. Connect Google Assistant to your middleware
You’ve got two main options here, depending on your use case:
- Actions on Google (now part of Google Actions):Best if you want to create a shareable, polished experience (e.g., for your team to use on their phones/smart speakers). You’ll define voice intents (like "Hey Google, trigger the backend build on Jenkins"), then link those intents to your middleware API via the fulfillment section of the Actions Console.
- Google Assistant SDK:Ideal for personal use or custom devices (like a Raspberry Pi-powered assistant). You can write a Python script that listens for specific voice commands, then directly calls your middleware (or even the Jenkins API, though middleware is still better for security).
3. Lock down security
Don’t skip this part—external APIs like Jenkins have sensitive operations:
- For Jenkins: Use an API token (not your password) and restrict the user’s permissions to only what’s needed (e.g., build trigger, job disable, no delete access).
- For middleware: Add API key authentication to block unauthorized requests, use HTTPS for all traffic, and store secrets in environment variables (never hardcode them!).
- For Google Assistant: If using Actions, limit intent access to your team if needed; if using the SDK, keep the script running on a secure, private network.
It depends on your goals:
Use this stack if:
- You’re building a personal tool (e.g., only for your own use on a local device).
- You need full control over the code and want to quickly prototype custom logic (like combining Jenkins calls with other local scripts).
- You’re comfortable maintaining a Python script running in the background.
Consider Actions on Google instead if:
- You want to share the app with your team or other users (it works across all Google Assistant devices).
- You don’t want to manage a persistent script—Google hosts most of the infrastructure for Actions.
- You need advanced voice interaction features (like multi-turn conversations, intent refinement, or user account linking).
- Test your middleware first: Use Postman or
curlto make sure it can trigger Jenkins builds/disables before connecting it to Google Assistant. - Add error handling with voice-friendly messages: Instead of "HTTP 500 error", tell users something like "Sorry, I couldn’t reach the Jenkins server right now—can you check if it’s running?"
- For the SDK, use the
google-assistant-sdkPython package—it’s well-documented and makes listening for commands straightforward.
内容的提问来源于stack exchange,提问作者Sunny

