Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

<<<<<<< HEAD

πŸ€– AI Triage Bot β€” Powered by Google Gemini + ServiceNow

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.


🧠 What Does This Bot Actually Do?

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:

  1. It connects to ServiceNow β€” a popular IT ticketing platform β€” and grabs the 5 most recent support tickets
  2. 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
  3. 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.


🧩 The Two APIs This Bot Uses

1. ServiceNow API

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.

2. Google Gemini API

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.


πŸ“ File Structure

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!

βš™οΈ How To Set It Up From Scratch

Don't worry if you're new to this β€” we'll go step by step.

Step 1 β€” Make sure you have Python installed

Open your terminal and run:

python3 --version

You should see something like Python 3.11.x. If not, install it via Homebrew:

brew install python

Step 2 β€” Create a virtual environment

A 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/activate

You'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" >> ~/.zshrc

Step 3 β€” Install the required packages

This bot needs two external Python libraries:

pip install google-genai requests
  • google-genai β€” the official Google SDK for talking to Gemini
  • requests β€” 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.txt

Step 4 β€” Get your API keys

Google Gemini API Key:

  1. Go to aistudio.google.com
  2. Sign in with your Google account
  3. Click Get API Key β†’ Create API Key
  4. Copy it somewhere safe

ServiceNow Developer Instance:

  1. Go to developer.servicenow.com
  2. Create a free account
  3. Request a Personal Developer Instance (PDI)
  4. Note your instance URL (e.g. https://dev215999.service-now.com), username (admin), and password

Step 5 β€” Store your secrets safely (IMPORTANT!)

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 ~/.zshrc

Your 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. βœ…

Step 6 β€” Run the bot!

python3 ./triage_bot2.py

πŸ” How The Code Works β€” Section by Section

Config block

SNOW_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.


Fetching tickets from ServiceNow

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.


Asking Gemini to summarize each ticket

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.


Printing the table

def print_table(rows):

This function formats everything into a neat table with columns and dividers, printed directly in your terminal.


The main function

def main():

This is the glue that ties everything together:

  1. Calls get_latest_incidents() to fetch tickets
  2. Loops through each ticket
  3. Calls ai_summary() on each one
  4. Collects the results
  5. Calls print_table() to display them

🌱 How To Build Something Like This Yourself

Here's the general recipe for building any "fetch data + AI summarize" bot:

  1. Find an API that has data you care about (tickets, emails, news, orders, etc.)
  2. Use requests to fetch data from that API
  3. Write a system prompt that tells Gemini exactly what to do with that data
  4. Ask Gemini to return JSON so you can easily work with the response
  5. 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.


πŸš€ Ideas For Extending This Bot

  • 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 cron on your Mac to run the bot every hour automatically

πŸ›‘οΈ Security Checklist Before Pushing to GitHub

  • No passwords or API keys hardcoded in the script
  • All secrets stored in ~/.zshrc as environment variables
  • .env added to .gitignore
  • Double-checked with grep -i "password\|api_key\|secret" triage_bot2.py

πŸ‘€ Author

viklall β€” github.com/viklall


πŸ“„ License

MIT β€” free to use, modify, and share.

gemini-servicenow

Python script to use Gemini to summarize incident tickets in a ServiceNow PDI

824b2329d4f7a5920ce61a543cffeed02de80ae4

About

Python script to use Gemini to summarize incident tickets in a ServiceNow PDI

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages