Created
June 3, 2018 10:03
-
-
Save sakakendo/0f2d9aff6d2903e6891b8d7e36260674 to your computer and use it in GitHub Desktop.
wikipedia bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from apscheduler.schedulers.background import BackgroundScheduler | |
#from apscheduler.schedulers.blocking import BlockingScheduler | |
from xml.dom import minidom | |
import os,time,logging,re,datetime,requests,json,random | |
from bs4 import BeautifulSoup | |
from slackclient import SlackClient | |
from pytz import utc | |
import wikipedia | |
logging.basicConfig(filename='info.log',level=logging.INFO) | |
sched=BackgroundScheduler(timezone=utc) | |
slack_token=os.getenv('SLACK_TOKEN') | |
sc=SlackClient(slack_token) | |
#genre={'History':0,'Computing':1,'Education':2,'Engineering and technology':3,'Mathematics':4} | |
def get_articles(genre='History'): | |
return [] | |
def randomTitle(fname='articles'): | |
lines=0 | |
with open(fname) as f: | |
while f.readline() :lines+=1 | |
with open(fname) as f: | |
for i in range(random.randint(0,lines)): | |
title=f.readline() | |
return title | |
def scheduled_job(mes=None,event=None): | |
print("This job is run every weekday at 8am.") | |
if mes is not None:greet(mes) | |
while True: | |
title=randomTitle('articles') | |
page=wikipedia.page(title) | |
try: | |
summary=wikipedia.summary(title) | |
except wikipedia.exceptions.DisambiguationError as e: | |
print('wikipedia throwing error') | |
continue | |
if event==None: | |
greet("today's article",attach=json.dumps([{"title":page.title},{"text":page.url},{"text":summary}])) | |
else: | |
react(event,mes="today's article", | |
attach=json.dumps([{"title":page.title},{"text":page.url},{"text":summary}])) | |
break | |
def greet(message,attach=None,channel="CAPB3A88G"): | |
ret=sc.api_call("chat.postMessage",channel=channel,text=message,attachments=attach) | |
print(ret) | |
logging.info("send message : "+message+str(ret)) | |
def react(event,mes,attach=None): | |
print(sc.api_call('chat.postMessage',channel=event.get('channel'),text=mes,attachments=attach)) | |
def reply(event,mes,attach=None): | |
pass | |
def groupsList(): | |
ret=sc.api_call("groups.list") | |
for group in ret.get('groups'): | |
print('id',group.get('id'),'name',group.get('name')) | |
def users_list(): | |
ret=sc.api_call("users.list") | |
print(ret) | |
def process(events): | |
if events == [] or len(events)<=0:return | |
print(events) | |
if events[0].get('type') =='message': | |
if events[0].get('text') == 'h': | |
react(events[0],mes="this bot is developed by sakakendo.", | |
attach=json.dumps([{"text":"h:help\nr:random"}])) | |
elif events[0].get('text') == 'r': | |
scheduled_job(event=events[0]) | |
if __name__ == '__main__': | |
greet('app deployed') | |
#sched.add_job(scheduled_job,'cron',hour="22",minute="0") #jst am7 | |
sched.add_job(scheduled_job,'cron',hour="23",minute="0") #jst am8 | |
sched.start() | |
if sc.rtm_connect(): | |
while True: | |
process(sc.rtm_read()) | |
time.sleep(1) | |
print('sc server refused') | |
else: | |
print('connection failed') | |
sched.shutdown() | |
greet('exit application') | |
exit() | |
print('task finished') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment