-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbot.py
164 lines (135 loc) · 4.87 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright (c) 2023 - 2024, Owners of https://github.com/autogenhub
#
# SPDX-License-Identifier: Apache-2.0
#
# Portions derived from https://github.com/microsoft/autogen are under the MIT License.
# SPDX-License-Identifier: MIT
import logging
import logging.handlers
import os
import discord
from agent_utils import solve_task
from discord.ext import commands
logger = logging.getLogger("anny")
logger.setLevel(logging.INFO)
logging.getLogger("discord.http").setLevel(logging.INFO)
handler = logging.handlers.RotatingFileHandler(
filename="autoanny.log",
encoding="utf-8",
maxBytes=32 * 1024 * 1024, # 32 MiB
backupCount=5, # Rotate through 5 files
)
dt_fmt = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(
"[{asctime}] [{levelname:<8}] {name}: {message}", dt_fmt, style="{"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
required_env_vars = ["OAI_CONFIG_LIST", "DISCORD_TOKEN", "GH_TOKEN", "ANNY_GH_REPO"]
for var in required_env_vars:
if var not in os.environ:
raise ValueError(f"{var} environment variable is not set.")
# read token from environment variable
DISCORD_TOKEN = os.environ["DISCORD_TOKEN"]
REPO = os.environ["ANNY_GH_REPO"]
intents = discord.Intents.default()
intents.message_content = True
intents.reactions = True
bot = commands.Bot(command_prefix="/", intents=intents)
@bot.event
async def on_message(message):
logger.info(
{"message": message.content, "author": message.author, "id": message.id}
)
await bot.process_commands(message)
@bot.event
async def on_reaction_add(reaction, user):
message = reaction.message
logger.info(
{
"message": message.content,
"author": message.author,
"id": message.id,
"reaction": reaction.emoji,
"reactor": user,
}
)
@bot.event
async def on_ready():
logger.info("Logged in", extra={"user": bot.user})
@bot.command(description="Invoke Anny to solve a task.")
async def heyanny(ctx, task: str = None):
if not task or task == "help":
response = help_msg()
await ctx.send(response)
return
task_map = {
"ghstatus": ghstatus,
"ghgrowth": ghgrowth,
"ghunattended": ghunattended,
"ghstudio": ghstudio,
}
if task in task_map:
await ctx.send("Working on it...")
response = await task_map[task](ctx)
await ctx.send(response)
else:
response = (
"Invalid command! Please type /heyanny help for the list of commands."
)
await ctx.send(response)
def help_msg():
response = f"""
Hi this is Anny an AutoGen-powered Discord bot to help with `{REPO}`. I can help you with the following tasks:
- ghstatus: Find the most recent issues and PRs from today.
- ghgrowth: Find the number of stars, forks, and indicators of growth.
- ghunattended: Find the most issues and PRs from today from today that haven't received a response/comment.
You can invoke me by typing `/heyanny <task>`.
"""
return response
async def ghstatus(ctx):
response = await solve_task(
f"""
Find the most recent issues and PRs from `{REPO}` in last 24 hours.
Separate issues and PRs.
Final response should contains title, number, date/time, URLs of the issues and PRs.
Markdown formatted response will make it look nice.
Make sure date/time is in PST and readily readable.
You can access github token from the environment variable called GH_TOKEN.
"""
)
return response
async def ghgrowth(ctx):
response = await solve_task(
f"""
Find the number of stars, forks, and indicators of growth of `{REPO}`.
Compare the stars of `{REPO}` this week vs last week.
Make sure date/time is in PST and readily readable.
You can access github token from the environment variable called GH_TOKEN.
"""
)
return response
async def ghunattended(ctx):
response = await solve_task(
f"""
Find the issues *created* in the last 24 hours from `{REPO}` that haven't
received a response/comment. Modified issues don't count.
Final response should contains title, number, date/time, URLs of the issues and PRs.
Make sure date/time is in PST and readily readable.
You can access github token from the environment variable called GH_TOKEN.
"""
)
return response
async def ghstudio(ctx):
# TODO: Generalize to feature name
response = await solve_task(
f"""
Find issues and PRs from `{REPO}` that are related to the AutoGen Studio.
The title or the body of the issue or PR should give you a hint whether its related.
Summarize the top 5 common complaints or issues. Cite the issue/PR number and URL.
Explain why you think this is a common issue in 2 sentences.
You can access github token from the environment variable called GH_TOKEN.
"""
)
return response
bot.run(DISCORD_TOKEN, log_handler=None)