lunarflu HF staff commited on
Commit
ad9d692
·
verified ·
1 Parent(s): 85db3fb

testing daily pings

Browse files
Files changed (1) hide show
  1. app.py +40 -8
app.py CHANGED
@@ -5,6 +5,8 @@ import gradio as gr
5
  from discord.ext import commands
6
  from slack_sdk import WebClient
7
  from slack_sdk.errors import SlackApiError
 
 
8
 
9
  DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
10
  SLACK_BOT_TOKEN = os.getenv('BOT_USER_OAUTH_TOKEN_HF')
@@ -12,6 +14,7 @@ SLACK_BOT_TOKEN = os.getenv('BOT_USER_OAUTH_TOKEN_HF')
12
  # real = os.getenv('SLACK_CHANNEL_ID_HF')
13
  # test = 'C07B4KNU5BQ'
14
  SLACK_CHANNEL_ID = os.getenv('SLACK_CHANNEL_ID_HF')
 
15
  # 1259415803879751700 = test forum
16
  # 1019883044724822016 = ask for help
17
  ASK_FOR_HELP_CHANNEL_ID = 1019883044724822016
@@ -42,6 +45,8 @@ TRIGGERS = {
42
  ("dataset", "feedback"): "<@U0768RCHCRY>", # ben burtenshaw
43
  }
44
 
 
 
45
  intents = discord.Intents.all()
46
  intents.messages = True
47
  bot = commands.Bot(command_prefix='!', intents=intents)
@@ -64,13 +69,20 @@ async def on_message(message):
64
  # notification bot
65
  huggingfolks_role = discord.utils.get(message.guild.roles, id=897376942817419265)
66
  bots_role = discord.utils.get(message.guild.roles, id=1258328471609016341)
67
- if huggingfolks_role not in message.author.roles: # no need for ping if we're already discussing
68
- if bots_role not in message.author.roles: # bots shouldn't trigger pings for this
69
- content = message.content.lower()
70
- for trigger, slack_mention in TRIGGERS.items():
71
- if all(word in content for word in trigger):
72
- await post_to_slack(message.author, message.content, message.channel.name, message.jump_url, slack_mention, trigger)
73
- break
 
 
 
 
 
 
 
74
 
75
  # Check if the message is in a thread
76
  if isinstance(message.channel, discord.Thread):
@@ -127,8 +139,27 @@ def post_to_slack_create_thread(channel, text, thread_ts=None):
127
  print(f"Error posting to Slack: {e.response['error']}")
128
  return None
129
 
130
- #----------------------------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
 
132
 
133
  async def post_to_slack(author, content, channel, url, slack_mention, trigger):
134
  try:
@@ -142,6 +173,7 @@ async def post_to_slack(author, content, channel, url, slack_mention, trigger):
142
 
143
  # runs discord bot in thread = helps avoid blocking calls
144
  def run_bot():
 
145
  bot.run(DISCORD_TOKEN)
146
  threading.Thread(target=run_bot).start()
147
  def greet(name):
 
5
  from discord.ext import commands
6
  from slack_sdk import WebClient
7
  from slack_sdk.errors import SlackApiError
8
+ import aiojobs
9
+ from datetime import datetime, timedelta
10
 
11
  DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
12
  SLACK_BOT_TOKEN = os.getenv('BOT_USER_OAUTH_TOKEN_HF')
 
14
  # real = os.getenv('SLACK_CHANNEL_ID_HF')
15
  # test = 'C07B4KNU5BQ'
16
  SLACK_CHANNEL_ID = os.getenv('SLACK_CHANNEL_ID_HF')
17
+ SLACK_CHANNEL_ID_TEST = 'C07B4KNU5BQ'
18
  # 1259415803879751700 = test forum
19
  # 1019883044724822016 = ask for help
20
  ASK_FOR_HELP_CHANNEL_ID = 1019883044724822016
 
45
  ("dataset", "feedback"): "<@U0768RCHCRY>", # ben burtenshaw
46
  }
47
 
48
+ daily_pings = []
49
+
50
  intents = discord.Intents.all()
51
  intents.messages = True
52
  bot = commands.Bot(command_prefix='!', intents=intents)
 
69
  # notification bot
70
  huggingfolks_role = discord.utils.get(message.guild.roles, id=897376942817419265)
71
  bots_role = discord.utils.get(message.guild.roles, id=1258328471609016341)
72
+ #if huggingfolks_role not in message.author.roles: # no need for ping if we're already discussing
73
+ if bots_role not in message.author.roles: # bots shouldn't trigger pings for this
74
+ content = message.content.lower()
75
+ for trigger, slack_mention in TRIGGERS.items():
76
+ if all(word in content for word in trigger):
77
+ daily_pings.append({
78
+ 'author': str(message.author),
79
+ 'content': message.content,
80
+ 'channel': message.channel.name,
81
+ 'url': message.jump_url,
82
+ 'mention': slack_mention,
83
+ 'trigger': trigger
84
+ })
85
+ break
86
 
87
  # Check if the message is in a thread
88
  if isinstance(message.channel, discord.Thread):
 
139
  print(f"Error posting to Slack: {e.response['error']}")
140
  return None
141
 
142
+ #----------------------------------------------------------------------------------------------
143
+ async def collect_pings():
144
+ await bot.wait_until_ready()
145
+ while not bot.is_closed():
146
+ await aiojobs.create_scheduler().spawn(send_daily_pings())
147
+ await asyncio.sleep(60)
148
+
149
+ async def send_daily_pings():
150
+ global daily_pings
151
+ if daily_pings:
152
+ # combine into one message
153
+ combined_message = '\n'.join(f"{ping['author']} in #{ping['channel']} said: {ping['content']} (link: {ping['url']})" for ping in daily_pings)
154
+ await post_to_slack(None, combined_message, SLACK_CHANNEL_ID_TEST, None, None, None)
155
+ daily_pings = [] # reset after posting
156
+
157
+
158
+
159
+
160
+
161
 
162
+ # pings -------------------------------------------------------------------------------------------
163
 
164
  async def post_to_slack(author, content, channel, url, slack_mention, trigger):
165
  try:
 
173
 
174
  # runs discord bot in thread = helps avoid blocking calls
175
  def run_bot():
176
+ bot.loop.create_task(collect_pings())
177
  bot.run(DISCORD_TOKEN)
178
  threading.Thread(target=run_bot).start()
179
  def greet(name):