File size: 5,210 Bytes
78dea1a
db21dc3
434f798
db21dc3
78dea1a
b7eca85
 
 
4b7b722
56509cf
 
0654423
b7eca85
 
a368445
d448043
 
 
 
 
 
 
 
780b804
 
 
d448043
 
 
 
597b324
 
30b066f
 
780b804
 
 
b7eca85
 
78dea1a
 
 
b7eca85
75d2f81
b7eca85
78dea1a
 
 
b7eca85
78dea1a
 
 
 
b7eca85
9177836
e57e72d
b91f995
e57e72d
b91f995
 
 
 
 
 
b7eca85
cb7cea6
0654423
 
 
 
 
90c7959
0654423
 
 
90c7959
0654423
 
 
 
 
 
90c7959
0654423
 
 
 
 
90c7959
0654423
 
 
 
 
 
 
 
90c7959
3c830fa
90c7959
3c830fa
 
 
90c7959
3c830fa
 
 
90c7959
 
 
 
 
 
 
 
3c830fa
 
 
 
 
 
 
90c7959
0654423
 
 
 
 
 
9177836
 
a368445
b7eca85
 
7b3eccf
9e52f3a
b7eca85
 
 
 
0654423
d4ce55b
db21dc3
 
 
 
 
 
 
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
import discord
import threading
import os
import gradio as gr
from discord.ext import commands
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
SLACK_BOT_TOKEN = os.getenv('BOT_USER_OAUTH_TOKEN_HF')
SLACK_CHANNEL_ID = os.getenv('SLACK_CHANNEL_ID_HF')
SLACK_CHANNEL_ID_TEST = 'C07B4KNU5BQ'

TRIGGERS = {
    "discord bot": "<@U051DB2754M>", # Adam
    "text to speech": "<@U039C2GANMV>", # VB
    "tts": "<@U039C2GANMV>", # VB
    "asr": "<@U039C2GANMV>", # VB
    "musicgen": "<@U039C2GANMV>", # VB
    "whisper": "<@U039C2GANMV>", # VB
    "speech recognition": "<@U039C2GANMV>", # VB
    "bark": "<@U039C2GANMV>", # VB
    "autotrain": "<@U01E3LEC2N7>", # abhishek
    "auto train": "<@U01E3LEC2N7>", # abhishek
    "competition": "<@U01E3LEC2N7>", # abhishek
    "competitions": "<@U01E3LEC2N7>", # abhishek
    "sentence-transformers": "<@U04E4DNPWG7>", # tom aarsen
    "sentence_transformers": "<@U04E4DNPWG7>", # tom aarsen
    "setfit": "<@U04E4DNPWG7>", # tom aarsen
    "sentence transformers": "<@U04E4DNPWG7>", # tom aarsen
    "argilla": "<@U076B8C7G3E>", # david berenstein
    "distilabel": "<@U076B8C7G3E>", # david berenstein
    "argilla": "<@U0766H30T7F>", # natalia elvira
    "dataset": "<@U0766H30T7F>", # natalia elvira
    "docs": "<@U02DATT4C5B>", # steven liu
    "documentation": "<@U02DATT4C5B>", # steven liu
    
}

intents = discord.Intents.all()
intents.messages = True
bot = commands.Bot(command_prefix='!', intents=intents)

slack_client = WebClient(token=SLACK_BOT_TOKEN)

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user}')

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    # notification bot
    huggingfolks_role = discord.utils.get(message.guild.roles, id=897376942817419265)
    bots_role = discord.utils.get(message.guild.roles, id=1258328471609016341)
    if huggingfolks_role not in message.author.roles: # no need for ping if we're already discussing
        if bots_role not in message.author.roles: # bots shouldn't trigger pings for this
            content = message.content.lower()
            for trigger, slack_mention in TRIGGERS.items():
                if trigger in content:
                    await post_to_slack(message.author, message.content, message.channel.name, message.jump_url, slack_mention, trigger)
                    break

    ask_for_help = await bot.fetch_channel(1259415803879751700)
    if ask_for_help.last_message_id == message.channel.id:
        content = message.content
        thread_id = message.channel.id

        try:

            thread_ts = find_slack_thread_ts(thread_id)

            if thread_ts:

                slack_client.chat_postMessage(
                    channel=SLACK_CHANNEL_ID_TEST,
                    text=content,
                    thread_ts=thread_ts
                )
            else:

                response = slack_client.chat_postMessage(
                    channel=SLACK_CHANNEL_ID_TEST,
                    text=f"New post in #ask-for-help by {message.author}: *{message.channel.name}*\n{message.jump_url}\n\nDiscord Thread ID: {thread_id}"
                )
                thread_ts = response['ts']

                slack_client.chat_postMessage(
                    channel=SLACK_CHANNEL_ID_TEST,
                    text=content,
                    thread_ts=thread_ts
                )
        except SlackApiError as e:
            print(f"Error posting to Slack: {e.response['error']}")


def find_slack_thread_ts(thread_id):
    """Search for a Slack thread with the given Discord thread ID and return the thread_ts."""
    try:
        response = slack_client.conversations_history(
            channel=SLACK_CHANNEL_ID_TEST,
            limit=200 
        )
        messages = response['messages']

        while response['has_more']:
            response = slack_client.conversations_history(
                channel=SLACK_CHANNEL_ID_TEST,
                limit=200,
                cursor=response['response_metadata']['next_cursor']
            )
            messages.extend(response['messages'])

        for message in messages:
            if f"Discord Thread ID: {thread_id}" in message['text']:
                return message['ts']

    except SlackApiError as e:
        print(f"Error fetching Slack messages: {e.response['error']}")

    return None



    #----------------------------------------------------------------------------------------------




async def post_to_slack(author, content, channel, url, slack_mention, trigger):
    try:
        response = slack_client.chat_postMessage(
            channel=SLACK_CHANNEL_ID,
            text=f"{slack_mention} (for the keyword -> '{trigger}')\nFrom {author} in channel #{channel}: {content}\n{url}"
        )
    except SlackApiError as e:
        print(f"Error posting to Slack: {e.response['error']}")


# runs discord bot in thread = helps avoid blocking calls
def run_bot():
    bot.run(DISCORD_TOKEN)
threading.Thread(target=run_bot).start()
def greet(name):
    return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()