Stages 1 + 2 complete
This commit is contained in:
		
							parent
							
								
									19db2a16fe
								
							
						
					
					
						commit
						d083e79d8e
					
				
					 5 changed files with 95 additions and 0 deletions
				
			
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					data.json
 | 
				
			||||||
							
								
								
									
										0
									
								
								commands.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								commands.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										27
									
								
								data-example.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								data-example.json
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,27 @@
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    "username":"bot",
 | 
				
			||||||
 | 
					    "password":"hunter2",
 | 
				
			||||||
 | 
					    "homeserver":"http://localhost:8008",
 | 
				
			||||||
 | 
					    "faq":{
 | 
				
			||||||
 | 
					        "header":"# Simple example FAQ",
 | 
				
			||||||
 | 
					        "questions":[
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                "question":"# What is the meaning of life?",
 | 
				
			||||||
 | 
					                "answer":"42.",
 | 
				
			||||||
 | 
					                "key_phrases":["meaning","life"]
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                "question":"# Who is the coolest pokemon?",
 | 
				
			||||||
 | 
					                "answer":"Mewtwo! 🐈",
 | 
				
			||||||
 | 
					                "key_phrases":["coolest","pokemon"]
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                "question":"# What is the coolest programming language?",
 | 
				
			||||||
 | 
					                "answer":"🐍 python!",
 | 
				
			||||||
 | 
					                "key_phrases":["coolest","programming","language"]
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					        "footer":"Brought to you by Nerve\nSee the [code](https://codeberg.org/gabe/Nerve)"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										66
									
								
								main.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								main.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,66 @@
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					import nio
 | 
				
			||||||
 | 
					import simplematrixbotlib as botlib
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					data = json.loads(open('data.json').read())
 | 
				
			||||||
 | 
					dfaq = data["faq"]
 | 
				
			||||||
 | 
					faq_text = dfaq["header"] + "".join(["\n"+q["question"]+"\n"+q["answer"]
 | 
				
			||||||
 | 
					                                    for q in dfaq["questions"]]) + "\n\n"+dfaq["footer"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					creds = botlib.Creds(
 | 
				
			||||||
 | 
					        data["homeserver"],
 | 
				
			||||||
 | 
					        data['username'],
 | 
				
			||||||
 | 
					        data['password']
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					bot = botlib.Bot(creds)
 | 
				
			||||||
 | 
					PREFIX = '!'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def load_faq():
 | 
				
			||||||
 | 
					    dfaq = json.loads(open('data.json').read())["faq"]
 | 
				
			||||||
 | 
					    return dfaq["header"] + "".join(["\n"+q["question"]+"\n"+q["answer"] for q in dfaq["questions"]]) + "\n\n"+dfaq["footer"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@bot.listener.on_message_event
 | 
				
			||||||
 | 
					async def faq(room, message):
 | 
				
			||||||
 | 
					    match = botlib.MessageMatch(room, message, bot, PREFIX)
 | 
				
			||||||
 | 
					    if match.is_not_from_this_bot() and match.prefix() and match.command(
 | 
				
			||||||
 | 
					            "faq"):
 | 
				
			||||||
 | 
					        await bot.api.send_markdown_message(
 | 
				
			||||||
 | 
					            room.room_id,
 | 
				
			||||||
 | 
					            load_faq())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@bot.listener.on_custom_event(nio.InviteMemberEvent)
 | 
				
			||||||
 | 
					async def autofaq(room, event):
 | 
				
			||||||
 | 
					    if event.membership == "join":
 | 
				
			||||||
 | 
					        await bot.api.send_markdown_message(
 | 
				
			||||||
 | 
					            room.room_id,
 | 
				
			||||||
 | 
					            load_faq()
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def get_questions():
 | 
				
			||||||
 | 
					    return json.loads(open('data.json').read())["faq"]["questions"]
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@bot.listener.on_message_event
 | 
				
			||||||
 | 
					async def faqresponse(room, message):
 | 
				
			||||||
 | 
					    match = botlib.MessageMatch(room, message, bot, PREFIX)
 | 
				
			||||||
 | 
					    if match.is_not_from_this_bot():
 | 
				
			||||||
 | 
					        response = ""
 | 
				
			||||||
 | 
					        questions = get_questions()
 | 
				
			||||||
 | 
					        print("Message:",message)
 | 
				
			||||||
 | 
					        for q in questions:
 | 
				
			||||||
 | 
					            if sum([ 1 for kp in q["key_phrases"] if kp in message.body]) == len(q["key_phrases"]):
 | 
				
			||||||
 | 
					                response = response + q["question"] + "\n" + q["answer"]+"\n"
 | 
				
			||||||
 | 
					        if response != "":
 | 
				
			||||||
 | 
					            await bot.api.send_markdown_message(
 | 
				
			||||||
 | 
					                room.room_id,
 | 
				
			||||||
 | 
					                response
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bot.run()
 | 
				
			||||||
							
								
								
									
										1
									
								
								session.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								session.txt
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					b'gAAAAABiFeXXt9WhUOMhWlxqijC3oTqJQNEDa4ulqGG2V5TRO45uGBye2P2GL7B2Kg8W4UQDkcIG2Ihx6dX4ViTpWnlZx0oF-g=='
 | 
				
			||||||
		Loading…
	
		Reference in a new issue