Skip to content

Instantly share code, notes, and snippets.

@EYHN
Created September 21, 2017 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EYHN/ae4c674f07eafae247747665c7d3c9b1 to your computer and use it in GitHub Desktop.
Save EYHN/ae4c674f07eafae247747665c7d3c9b1 to your computer and use it in GitHub Desktop.
Python github forward telegram bot
from telegram import Bot,ParseMode
from github import Github
import time
from datetime import datetime
import calendar
import traceback
bot = Bot("xxxxxxxx:xxxxxxxxxxxxxxxxxxxxxx") #Telegram bot token
def newEvent(event):
if event.type == "WatchEvent" and event.public:
bot.sendMessage(chat_id="@channel_id", text=("#Github转发\n" #Telegram channel id
"[{actor}]({actorLink}) starred [{repo}]({repoLink})"
).format(actor=event.actor.login, actorLink=event.actor.html_url,
repo=event.repo.name,
repoLink=event.repo.html_url), parse_mode=ParseMode.MARKDOWN)
print event.type
def main():
lastMessageDate = None
gayhub = Github("username","password") #Github username and password
user = gayhub.get_user("username") #same as last line
while True:
try:
events = user.get_events()
if events[0]:
if lastMessageDate is None:
lastMessageDate = events[0].created_at
if events[0].created_at > lastMessageDate:
lastMessageDate = events[0].created_at
newEvent(events[0])
except:
traceback.print_exc()
sleeptime = float(gayhub.rate_limiting_resettime - calendar.timegm(datetime.utcnow().timetuple())) / float(gayhub.rate_limiting[0])
try:
time.sleep(max(sleeptime, 20))
except:
break
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment