<<<<<<< HEAD
A beginner-friendly Python script that automatically fetches IT support tickets from ServiceNow and uses Google's Gemini AI to summarize and triage them in seconds.
Imagine you work at an IT helpdesk. Every day, dozens of support tickets come in from employees β things like "my laptop won't connect to Wi-Fi" or "I need Oracle installed." Reading every ticket and figuring out what's urgent takes time.
This bot automates that. Here's what happens when you run it:
- It connects to ServiceNow β a popular IT ticketing platform β and grabs the 5 most recent support tickets
- It sends each ticket to Google Gemini β an AI model β and asks it to read the ticket and write a clear, one-sentence plain-English summary
- It prints everything out as a neat table in your terminal, like this:
INC β Customer β Short Description β AI Summary
ββββββββββββΌβββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββ
INC0000010 β Fred Luddy β Need Oracle 10GR2 installed β Employee needs Oracle database softwareβ¦
INC0000011 β Don Goodliffe β Need new Blackberry set up β User needs help configuring a new deviceβ¦
That's it! Two APIs, one Python script, a beautiful table of AI-triaged tickets.
ServiceNow is software that companies use to manage IT support tickets. It has an API (a way for code to talk to it) that lets you read, create, and update tickets programmatically.
In this bot, we use it to fetch the latest incidents (tickets) from a developer sandbox instance.
Gemini is Google's large language model (LLM) β similar to ChatGPT. It can read text and generate intelligent responses. In this bot, we send it each ticket and ask it to return a clean JSON object containing a one-sentence summary.
triage-bot/
βββ triage_bot2.py β The main script (this is what you run)
βββ requirements.txt β List of Python packages needed
βββ .gitignore β Tells git what NOT to upload (like secrets)
βββ README.md β This file!
Don't worry if you're new to this β we'll go step by step.
Open your terminal and run:
python3 --versionYou should see something like Python 3.11.x. If not, install it via Homebrew:
brew install pythonA virtual environment is like a clean, isolated bubble for your Python project. It keeps your packages separate from the rest of your Mac.
python3 -m venv ~/venv
source ~/venv/bin/activateYou'll know it's working when you see (venv) at the start of your terminal prompt.
To activate it automatically every time you open a terminal, run this once:
echo "source ~/venv/bin/activate" >> ~/.zshrcThis bot needs two external Python libraries:
pip install google-genai requestsgoogle-genaiβ the official Google SDK for talking to Geminirequestsβ a popular library for making HTTP requests (used to talk to ServiceNow)
You can also save these to a requirements.txt file so others can install them easily:
pip freeze > requirements.txtGoogle Gemini API Key:
- Go to aistudio.google.com
- Sign in with your Google account
- Click Get API Key β Create API Key
- Copy it somewhere safe
ServiceNow Developer Instance:
- Go to developer.servicenow.com
- Create a free account
- Request a Personal Developer Instance (PDI)
- Note your instance URL (e.g.
https://dev215999.service-now.com), username (admin), and password
Never put passwords or API keys directly in your code β especially if you're putting it on GitHub. Instead, store them as environment variables on your Mac.
Add these lines to your ~/.zshrc file:
echo 'export GOOGLE_API_KEY="your-gemini-key-here"' >> ~/.zshrc
echo 'export SNOW_PASS="your-servicenow-password-here"' >> ~/.zshrc
source ~/.zshrcYour script reads them automatically with:
os.environ.get("GOOGLE_API_KEY")
os.environ.get("SNOW_PASS")This way your secrets stay on your Mac and never end up on GitHub. β
python3 ./triage_bot2.pySNOW_INSTANCE = "https://dev215999.service-now.com"
SNOW_USER = "admin"
SNOW_PASS = os.environ.get("SNOW_PASS")This sets up the connection details for ServiceNow. The password is read from your environment variables, not hardcoded.
def get_latest_incidents(limit=5):This function makes an HTTP GET request to the ServiceNow API. It asks for the 5 most recent incidents, and returns them as a list of Python dictionaries (basically, structured data).
The auth=(SNOW_USER, SNOW_PASS) part is like logging in β ServiceNow checks your username and password before giving you any data.
def ai_summary(ticket_text: str) -> str:This function takes the raw ticket text, sends it to Gemini with a system prompt (instructions telling the AI how to behave), and gets back a JSON response containing a plain-English summary.
The system prompt tells Gemini:
"You are an IT triage assistant. Return ONLY a JSON object with a one-sentence summary."
Asking the AI to return JSON (structured data) instead of free-form text makes it easier to parse and display programmatically.
def print_table(rows):This function formats everything into a neat table with columns and dividers, printed directly in your terminal.
def main():This is the glue that ties everything together:
- Calls
get_latest_incidents()to fetch tickets - Loops through each ticket
- Calls
ai_summary()on each one - Collects the results
- Calls
print_table()to display them
Here's the general recipe for building any "fetch data + AI summarize" bot:
- Find an API that has data you care about (tickets, emails, news, orders, etc.)
- Use
requeststo fetch data from that API - Write a system prompt that tells Gemini exactly what to do with that data
- Ask Gemini to return JSON so you can easily work with the response
- Display or store the results however you like
The pattern is always: fetch β process with AI β display. Once you understand this loop, you can build bots for almost anything.
- Email alerts β send a summary email when a P1 (critical) incident comes in
- Slack integration β post the triage table to a Slack channel automatically
- Priority scoring β ask Gemini to rate each ticket 1-5 for urgency
- Auto-assignment β have Gemini suggest which team should handle each ticket
- Run on a schedule β use
cronon your Mac to run the bot every hour automatically
- No passwords or API keys hardcoded in the script
- All secrets stored in
~/.zshrcas environment variables -
.envadded to.gitignore - Double-checked with
grep -i "password\|api_key\|secret" triage_bot2.py
viklall β github.com/viklall
Python script to use Gemini to summarize incident tickets in a ServiceNow PDI
824b2329d4f7a5920ce61a543cffeed02de80ae4