내가 계속 개발하기를 원한다면 여기에 기부를 고려할 수 있습니다 : https://www.buymeacoffee.com/prxm
Reddit Chatrooms를위한 완벽한 이벤트 중심 챗봇 라이브러리!
Reddit Username & Password 또는 API 토큰과 함께 작동합니다 (등록 된 앱에서 얻은 일반적인 것은 아닙니다.
pip install Reddit-ChatBot-Python
필수의:
websocket_client
requests
모듈은 멀티 스레드 및 이벤트 중심입니다. 해당 이벤트의 데코레이터로 기능을 작성하여 이러한 이벤트에서 발생하는 일을 처리합니다.
Redditauthenticaton 인스턴스를 만들려면 두 가지 옵션이 있습니다.
권장 방법은 암호를 만드는 것입니다.
from Reddit_ChatBot_Python import RedditAuthentication
reddit_authentication = RedditAuthentication . PasswordAuth ( reddit_username = "" ,
reddit_password = "" ,
twofa = None )또는 자체 관찰 된 API 키와 함께 갈 수 있습니다. (등록 된 앱에서 얻은 것이 아닙니다)
from Reddit_ChatBot_Python import RedditAuthentication
reddit_authentication = RedditAuthentication . TokenAuth ( token = "" )그런 다음 reddit_authentication이 통과하여 챗봇을 인스턴스화합니다
from Reddit_ChatBot_Python import ChatBot
chatbot = ChatBot ( print_chat = True ,
store_session = True ,
log_websocket_frames = False ,
authentication = reddit_authentication
) print_chat 콘솔에서 채팅 메시지를 print 하는 것입니다.
store_session true로 설정하면 메인 스크립트가 호출 된 동일한 디렉토리에 Reddit_authentication을 저장하는 세션 파일을 만듭니다. 초기 연결 속도를 높입니다.
log_websocket_frames 콘솔에없는 비 정자 웹 소켓 프레임을 기록합니다
authentication 이전에 인스턴스화 한 RedditAuthentication 입니다
이벤트 :
@ chatbot . event . on_message정기적 인 채팅 메시지를받는 이벤트.
@ chatbot . event . on_ready처음으로 채팅에 연결하는 이벤트. 한 번만 실행되었습니다.
@ chatbot . event . on_invitation봇의 이벤트는 직접 또는 그룹이든 채팅 초대를받습니다.
@ chatbot . event . on_invitation_of_other다른 사용자가 봇이있는 그룹 채팅 초대를받는 이벤트.
@ chatbot . event . on_message_deleted사용자가 메시지를 삭제하는 이벤트.
@ chatbot . event . on_user_joined사용자가 봇이 채팅에 참여하는 이벤트.
@ chatbot . event . on_user_left봇이 채팅을 떠나는 사용자의 이벤트.
@ chatbot . event . on_user_typing사용자 입력 이벤트.
@ chatbot . event . on_user_read사용자가 메시지를 읽는 이벤트.
@ chatbot . event . on_broadcast사용자 가입시 방송을받는 이벤트
@ chatbot . event . on_reaction사용자가 반응을 보내는 사건
모든 이벤트는 인수로 구문 분석 프레임을받습니다. 그들은 이렇게 처리됩니다. :
@ chatbot . event . on_message
def greet ( resp ):
if resp . user . name == chatbot . get_own_name (): # return if the message is from the bot
return True
if resp . message == "Hi!" :
chatbot . send_message ( "Hello!" , resp . channel_url )
return True 이벤트에서 True 반환하면 다른 이벤트 처리가 수행되지 않음을 의미합니다!
resp.channel_url 우리가 메시지를 보내려는 채널의 URL입니다 (이 경우 resp.channel_url은 "hi!"메시지가 전송 된 채널입니다.)
resp 사용자와 메시지 문자열보다 더 많은 데이터를 전달합니다. 일부 이벤트에 대한 RESP 인스턴스는 다음과 같습니다.
남은 것이 챗봇을 시작하는 것입니다.
chatbot . run_4ever ( auto_reconnect = True )필드 액세스 방법 :
message = resp.message
nickname = resp.user.name
{
"msg_id" : 1000000 ,
"is_op_msg" : false ,
"is_guest_msg" : true ,
"message" : " msg " ,
"silent" : false ,
"ts" : 1611782454265 ,
"channel_url" : " sendbird_group_channel_000000000_0000000000000000000000000000000000000000 " ,
"is_removed" : false ,
"sts" : 1611782454265 ,
"user" : {
"is_blocked_by_me" : false ,
"require_auth_for_profile_image" : false ,
"name" : " *user nickname* " ,
"is_bot" : false ,
"image" : " " ,
"is_active" : true ,
"guest_id" : " t2_5z9rqylm " ,
"friend_discovery_key" : null ,
"role" : " " ,
"friend_name" : null ,
"id" : 10000
}
}{
"unread_cnt" : {
"all" : 1 ,
"ts" : 1614006345986
},
"is_super" : false ,
"data" : {
"inviter" : {
"nickname" : " *inviter nickname* " ,
"metadata" : {
},
"require_auth_for_profile_image" : false ,
"profile_url" : " " ,
"user_id" : " *user id str t2_ included* "
},
"invited_at" : 1614006345956 ,
"invitees" : [
{
"nickname" : " *bot's nickname* " ,
"metadata" : {
},
"require_auth_for_profile_image" : false ,
"profile_url" : " " ,
"user_id" : " t2_5z9rqylm "
}
]
},
"ts" : 1614006345978 ,
"is_access_code_required" : false ,
"cat" : 10020 ,
"channel_type" : " *can either be 'group' for group chat or 'direct' for DM* " ,
"channel_id" : 177639012 ,
"sts" : 1614006345978 ,
"channel_url" : " sendbird_group_channel_000000000_0000000000000000000000000000000000000000 "
}{
"is_super" : false ,
"data" : {
"is_bulk" : true ,
"users" : [
{
"require_auth_for_profile_image" : false ,
"nickname" : " nickname " ,
"role" : " " ,
"user_id" : " t2_5z9rqylm " ,
"inviter" : {
"user_id" : " t2_5z9rqylm " ,
"role" : " " ,
"require_auth_for_profile_image" : false ,
"nickname" : " nickname " ,
"profile_url" : " " ,
"metadata" : {
}
},
"profile_url" : " " ,
"metadata" : {
}
}
]
},
"ts" : 1614264797294 ,
"is_access_code_required" : false ,
"cat" : 10000 ,
"channel_type" : " group " ,
"channel_id" : 177639012 ,
"sts" : 1614264797294 ,
"channel_url" : " sendbird_group_channel_000000000_0000000000000000000000000000000000000000 "
}{
"channel_type" : " group " ,
"channel_id" : 177639012 ,
"is_super" : false ,
"channel" : {
"custom_type" : " group " ,
"is_ephemeral" : false ,
"freeze" : false ,
"disappearing_message" : {
"message_survival_seconds" : -1 ,
"is_triggered_by_message_read" : false
},
"member_count" : 2 ,
"is_broadcast" : false ,
"last_message" : null ,
"unread_mention_count" : 0 ,
"sms_fallback" : {
"wait_seconds" : -1 ,
"exclude_user_ids" : [
]
},
"is_discoverable" : false ,
"ignore_profanity_filter" : false ,
"channel_url" : " sendbird_group_channel_000000000_0000000000000000000000000000000000000000 " ,
"message_survival_seconds" : -1 ,
"unread_message_count" : 0 ,
"is_distinct" : false ,
"cover_url" : " https: // static.sendbird.com / sample / cover / cover_00.jpg " ,
"members" : [
{
"is_active" : true ,
"state" : " " ,
"user_id" : 10000000 ,
"is_online" : false ,
"is_muted" : false ,
"require_auth_for_profile_image" : false ,
"last_seen_at" : 0 ,
"nickname" : " nickname1 " ,
"profile_url" : " " ,
"metadata" : {
}
},
{
"is_active" : true ,
"state" : " " ,
"user_id" : 10000000 ,
"is_online" : false ,
"is_muted" : false ,
"require_auth_for_profile_image" : false ,
"last_seen_at" : 0 ,
"nickname" : " nickname2 " ,
"profile_url" : " " ,
"metadata" : {
}
}
],
"is_public" : false ,
"data" : " " ,
"joined_member_count" : 1 ,
"is_super" : false ,
"name" : " group name " ,
"created_at" : 1614264775 ,
"is_access_code_required" : false ,
"max_length_message" : 5000
},
"sts" : 1614265517558 ,
"channel_url" : " sendbird_group_channel_000000000_0000000000000000000000000000000000000000 " ,
"data" : {
"require_auth_for_profile_image" : false ,
"member_count" : 2 ,
"user_id" : " t2_5z9rqylm " ,
"joined_member_count" : 1 ,
"nickname" : " nickanme of user who left the group " ,
"profile_url" : " " ,
"metadata" : {
}
},
"ts" : 1614265517558 ,
"is_access_code_required" : false ,
"cat" : 10001
}{
"is_super" : false ,
"msg_id" : 2323966291 ,
"ts" : 1614266924885 ,
"channel_type" : " group " ,
"channel_id" : 177623421 ,
"sts" : 1614266924885 ,
"channel_url" : " sendbird_group_channel_000000000_0000000000000000000000000000000000000000 "
}{
"is_super" : false ,
"data" : {
"require_auth_for_profile_image" : false ,
"user_id" : " t2_xxxxxx " ,
"nickname" : " xxxx " ,
"profile_url" : " " ,
"metadata" : {
}
},
"ts" : 1617897296948 ,
"is_access_code_required" : false ,
"cat" : 10900 ,
"channel_type" : " group " ,
"channel_id" : 11111111 ,
"sts" : 1617897296948 ,
"channel_url" : " sendbird_group_channel_000000000_0000000000000000000000000000000000000000 "
} from Reddit_ChatBot_Python import ChatBot , RedditAuthentication
from Reddit_ChatBot_Python import CustomType , Snoo , Reaction
import random # for a basic dice rolling game
# create authentication with username and pass
reddit_authentication = RedditAuthentication . PasswordAuth ( reddit_username = "" , reddit_password = "" ,
twofa = None ) # 2FA supported, default'd to None
# instantiate the chatbot
chatbot = ChatBot ( print_chat = True , store_session = True , log_websocket_frames = False , # some parameters u might wanna know
authentication = reddit_authentication )
# you can add a rate limit like so:
chatbot . enable_rate_limiter ( max_calls = 23 , # how many messages will be sent by the bot
period = 1.5 # in what period (minutes)
)
# now you can add hooks which will be executed when a frame is received like so:
@ chatbot . event . on_message
def dice_roller ( resp ): # resp is a SimpleNamespace that carries all the data of the received frame
messg_s = resp . message . split ()
if messg_s [ 0 ] == "!roll" and len ( messg_s ) == 3 : # if received message says !roll
chatbot . send_reaction ( Reaction . REACT4 , resp . msg_id , resp . channel_url ) # send a reaction
limit_bottom = int ( messg_s [ 1 ])
limit_top = int ( messg_s [ 2 ])
rolled_number = random . randint ( limit_bottom , limit_top )
response_text = f"@ { resp . user . name } rolled { rolled_number } . Better luck next time!"
# send typing indicator cuz why not? maybe they think you are a real person
chatbot . send_typing_indicator ( resp . channel_url )
chatbot . send_message ( response_text ,
resp . channel_url ) # send the message, always add resp.channel_url as the second argument
chatbot . stop_typing_indicator ( resp . channel_url )
chatbot . send_snoomoji ( Snoo . PARTYPARROT , resp . channel_url ) # and send a snoomoji cuz why not??
return True # return true if you want to be done with checking the other hooks, otherwise return None or False
# keep in mind that first added hooks get executed first
# now everytime someone says "!roll 1 100", the bot will roll a dice between 1 and 100 and send the result!
# there are also host actions availabe but ofc they require the bot account to be the host of the chatroom
@ chatbot . event . on_message
def keeper_of_decency ( resp ):
if resp . message == "*some very bad slur word*" :
chatbot . kick_user ( channel_url = resp . channel_url , user_id = resp . user . guest_id , duration = 600 ) # duration is in secs
chatbot . send_message ( f'i banned { resp . user . name } for 10 mins' , resp . channel_url )
elif resp . message == "*another bad word*" :
chatbot . delete_mesg ( channel_url = resp . channel_url , msg_id = resp . msg_id )
chatbot . send_message ( f"i deleted { resp . user . name } 's message" , resp . channel_url )
# or you can add a basic response hook directly like so:
chatbot . set_respond_hook ( input_ = "Hi" , response = "Hello {nickname}! sup?" , limited_to_users = None , lower_the_input = False ,
exclude_itself = True , must_be_equal = True ,
limited_to_channels = [ "my cozy chat group" ]) # you can limit by indicating chatroom's name
# you can add a welcome message for newly joined users:
chatbot . set_welcome_message ( "welcome to the my cozy chat group u/{nickname}!)" ,
limited_to_channels = [ "my cozy chat group" ])
# and a farewell message too:
chatbot . set_farewell_message ( "Too bad u/{nickname} left us :(" , limited_to_channels = [ "my cozy chat group" ])
# there are also other types of hooks like this one for invitations
@ chatbot . event . on_invitation
def on_invit ( resp ):
if resp . channel_type == CustomType . group :
invit_type = "group chat"
elif resp . channel_type == CustomType . direct :
invit_type = "DM"
print ( f"got invited to { invit_type } by { resp . data . inviter . nickname } " )
chatbot . accept_chat_invite ( resp . channel_url )
chatbot . send_message ( "Hello! I accepted your invite" , resp . channel_url )
return True
# or on ready hook
@ chatbot . event . on_ready
def report_channels ( _ ):
channels = chatbot . get_channels ()
print ( "up and running in these channels!: " )
for channel in channels :
print ( channel . name )
# reading message history from a channel
@ chatbot . event . on_ready
def report_channels ( _ ):
channels = chatbot . get_channels ()
my_channel = None
for channel in channels :
if channel . name == "My Channel" :
my_channel = channel
last_hundred_messages = chatbot . get_older_messages ( channel_url = my_channel . channel_url ,
prev_limit = 100 )
last_created_at = last_hundred_messages [ - 1 ]. created_at
while True :
messages = chatbot . get_older_messages ( channel_url = my_channel . channel_url ,
message_ts = last_created_at ,
prev_limit = 30 )
last_created_at = messages [ - 1 ]. created_at
# starting a direct chat
@ chatbot . event . on_ready
def dm_chat ( _ ):
dm_channel = chatbot . create_direct_channel ( "someuseridk" )
chatbot . send_message ( "Hey what's up?" , dm_channel . channel_url )
# starting a group chat
@ chatbot . event . on_ready
def gorup_chat ( _ ):
group_channel = chatbot . create_channel ( nicknames = [ "user1" , "user2" ], group_name = "my group" )
chatbot . send_message ( "Hey guys what's up?" , group_channel . channel_url )
chatbot . invite_user_to_channel ( group_channel . channel_url , nicknames = [ "someotheruser" ])
invite_link = chatbot . get_invite_link ( group_channel . channel_url )
chatbot . send_message ( group_channel . channel_url , f"this is our invite link: { invite_link } " )
# wanna check invitations on start?
@ chatbot . event . on_ready
def check_invites ( _ ):
invites = chatbot . get_chat_invites ()
for invite in invites :
print ( f"invited to chat by { invite . inviter } with the message { invite . last_message . message } " )
chatbot . accept_chat_invite ( invite . channel_url )
return True
# and finally, run forever...
chatbot . run_4ever ( auto_reconnect = True )
# set auto_reconnect to True so as to re-connect in case remote server shuts down the connection after some period of time