Skip to content

Instantly share code, notes, and snippets.

View JosXa's full-sized avatar
💙

Joscha Götzer JosXa

💙
View GitHub Profile
def timeout(time_limit):
def decorator(func):
def timed_out(_signum, frame):
raise TestTimedOut(time_limit, frame)
def newfunc(*args, **kwargs):
try:
# Will only work on unix systems
def callback_router(bot, update, chat_data):
obj = json.loads(str(update.callback_query.data))
pprint(obj)
if 'a' in obj:
action = obj['a']
if action == CallbackActions.OVERVIEW:
finances.main(bot, update, chat_data)
elif action == CallbackActions.ALL_TRANSACTIONS:
finances.show_all(bot, update)
#!/bin/sh
userresources=$HOME/.Xresources
usermodmap=$HOME/.Xmodmap
sysresources=/etc/X11/xinit/.Xresources
sysmodmap=/etc/X11/xinit/.Xmodmap
# merge in defaults and keymaps
if [ -f $sysresources ]; then
@JosXa
JosXa / util.py
Last active February 6, 2017 19:06
def uid_from_update(update):
"""
Extract the user id from update
:param update: `telegram.Update`
:return: user_id extracted from the update
"""
user_id = None
try:
user_id = update.message.from_user.id
except (NameError, AttributeError):
def _can_vote_again(user):
"""
:param user: User to check vote_timeout for
:return: Tuple of (state, remaining_time) where state is a boolean
"""
if user.last_vote:
delta = datetime.timedelta(seconds=const.VOTE_TIMEOUT)
now = datetime.datetime.now()
difference = now - user.last_vote
to_wait = max((delta - difference).seconds, 1)
@JosXa
JosXa / bot.py
Last active May 3, 2017 22:36
Sending an audio file without actually downloading it to hard disk
def some_handler(bot, update):
chat_id = update.message.chat_id
import urllib.request
import io
url = "https://talkpython.fm/episodes/download/110/data-democratization-with-redash.mp3"
response = urllib.request.urlopen(url)
bytestring = response.read() # a `bytes` object
file_like_object = io.BytesIO(bytestring) # convert `bytes` to file-like object
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/python_telegram_bot-5.3.1-py3.6.egg/telegram/inputfile.py", line 83, in __init__
self.mimetype = self.is_image(self.input_file_content)
File "/usr/lib/python3.6/site-packages/python_telegram_bot-5.3.1-py3.6.egg/telegram/inputfile.py", line 164, in is_image
raise TelegramError('Could not parse file content')
telegram.error.TelegramError: Could not parse file content
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
#!/usr/bin/python3
import time
import random
from telethon import InteractiveTelegramClient
from telethon.tl.functions.photos import UploadProfilePhotoRequest
import os
client = InteractiveTelegramClient(
'test1',
'+400000000000',
@restricted(strict=True)
@run_async
def send_botlist(bot, update, resend=False, silent=False):
log.info("Re-Sending BotList..." if resend else "Updating BotList...")
chat_id = util.uid_from_update(update)
message_id = util.mid_from_update(update)
channel = helpers.get_channel()
error = False
def notify_admin(txt):
import datetime
import logging
from peewee import *
from telegram import Update
from telegram import Message as TelegramMessage
from telegram import Chat as TelegramChat
from telegram import User as TelegramUser
from model import User