一个相当基本的websocket包装器,用于reddit聊天室

Reddit-ChatBot-Python的Python项目详细描述


REDDIT正在杀死社区聊天功能。我来看看可以做些什么来进行群聊

Reddit聊天室

reddit聊天室的一个非常基本的websocket包装器

不含硒,不含牛粪,直接用websocket

使用api令牌(不是从注册的应用程序中获得的常规令牌),因此您不必公开您的通行证

有一个脚本可以转储sendbird访问范围内的令牌here

安装

pip install Reddit-ChatBot-Python

所需软件包:

^{pr2}$

用法

fromReddit_ChatBot_Python.ChatBotimportChatBot,RedditAuthenticationreddit_authentication=RedditAuthentication.PasswordAuth(reddit_username="",reddit_password="")# create authentication with username and passreddit_authentication=RedditAuthentication.TokenAuth(token="")# or you can directly authenticate with the self-obtained api token# either of them is OK# instantiate a chatbot and pass in the sub_channels if you wantchatbot=ChatBot(global_blacklist_words={'a','b'},# you can define words that shouldnt be sent (this migth be handy for slurs)global_blacklist_users={'a','b'},# hooks never get executed for users in this listprint_chat=True,store_session=True,dont_answer_blocked=True,# some parameters u might wanna knowauthentication=reddit_authentication)# grab the websocketwebsock=chatbot.WebSocketClient# you can add a rate limit like so:websock.RateLimiter.is_enabled=Truewebsock.RateLimiter.max_calls=23# how many messages will be sent by the botwebsock.RateLimiter.period=1.5# in what period(in minutes)# now you can add hooks to the websock object in order for them to be executed when a message is received like so:# create a function and hook@websock.after_message_hookdefroll(resp):#  resp is a namedtuple that carries all the data of the received frameifresp.type_f=="MESG":#  MESG is the type of the ordinary chat messages. you can see others here: https://github.com/scrubjay55/Reddit_ChatBot_Python/blob/master/Utils/FrameModel/FrameModel.py#  Dont forget to perform this check if you are just gonna do stuff with regular chat messagesmessg_s=resp.message.split()ifmessg_s[0]=="!roll"andlen(messg_s)==3:# if received message says !rolllimit_bottom=messg_s[1]limit_top=messg_s[2]rolled_number=random.randint(int(limit_f),int(limit_u))response_text=f"@{resp.user.name}{rolled_number}. Better luck next time!"# a basic roll gamewebsock.send_message(response_text,resp.channel_url)# and send the message, always add resp.channel_url as the second argumentwebsock.send_snoomoji('partyparrot',resp.channel_url)# and send a snoomoji cuz why notreturnTrue# return true if you want to be done with checking the other hooks, otherwise return None# keep in mind that first added hooks gets executed first# now everytime someone says "!roll 1 100", the bot will roll and send the result!# or you can add a basic response hook directly like so:websock.set_respond_hook(input_="Hi",response="Hello {nickname}! enjoy your time in r/askreddit chatroom",limited_to_users=None,lower_the_input=False,exclude_itself=True,must_be_equal=True,limited_to_channels=["AskReddit"])# you can add a welcome message for newly joined users too:websock.set_welcome_message("welcome to the r/askreddit chatroom u/{nickname}!",limited_to_channels=["AskReddit"])# you can limit by indicating chatroom's name# and finally, run forever...websock.run_4ever(auto_reconnect=True)# set auto_reconnect so as to re-connect in case remote server shuts down the connection after some period of time

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JavaFX触控事件未触发Ubuntu 20.04触控笔记本电脑   java如何在AWT中关闭窗口?   java Dagger 2:注入具有构造函数参数的成员   创建对象的Java调用类   对象我想在A.java中添加两个数字,并在B.java中打印结果(如何?)   java如何使用AWS SDK for Android从数字海洋空间下载图像?   java Facebook sdk 4.0.1无法使用Android studio获取某些字段   4分钟后web应用程序(Angular 8和Rest API)中的java自动会话超时   在Eclipse for Java EE developers edition中禁用HTML警告   java按字母顺序排列字符串我错过了什么明显的东西吗?   java在Jshell中println和printf有什么不同