-
-
Save meetmangukiya/4eebdf2218de8e4c40d19aaeead8aecf to your computer and use it in GitHub Desktop.
A script used to connect bot to all the members of the organization...
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
import requests | |
import logging | |
import sys | |
logging.basicConfig(level=logging.INFO) | |
API_GH = "" | |
API_Gitter = "" | |
SEND_MESSAGE = False | |
def join_gitter(username): | |
id_rq = requests.post("https://api.gitter.im/v1/rooms", | |
data={"uri": username}, | |
headers={"Authorization": "Bearer {}".format( | |
API_Gitter)}) | |
try: | |
room_id = id_rq.json()["id"] | |
join_rq = requests.post("https://api.gitter.im/v1/rooms", data={"id": room_id}, | |
headers={"Authorization": "Bearer {}".format(API_Gitter)}) | |
# send message that cobot is functional now on PC as well | |
if send_message: | |
msg_rq = requests.post( | |
"https://api.gitter.im/v1/rooms/{}/chatMessages".format(room_id), | |
data={ | |
"text": "Hello, coalaian!"+ "\n" + | |
"Good News! You can now use cobot on private chat as well!" | |
}, | |
headers = {"Authorization": "Bearer {}".format(API_Gitter)}) | |
logging.info("Joined room & sent bot message to {}".format(username)) | |
except KeyError: | |
logging.error("Cannot join room with {}".format(username)) | |
def get_users_and_join_gitter(link): | |
rq = requests.get(link, | |
headers = {"Authorization": "token {}".format(API_GH)}) | |
if 300 > rq.status_code >= 200: | |
for user in rq.json(): | |
join_gitter(user["login"]) | |
if "last" in rq.headers["Link"]: | |
link = rq.headers["Link"].split(",")[0].split(";")[0].strip("<|>") | |
logging.info("Pagination page {}".format(link.split("page=")[-1])) | |
get_users_and_join_gitter(link) | |
if sys.argv[-2] == "--user" or sys.argv[-2] == "-u": | |
join_gitter(sys.argv[-1]) | |
else: | |
get_users_and_join_gitter("https://api.github.com/orgs/coala/members") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment