Skip to content

Commit

Permalink
Update Telegram API and metafile writer
Browse files Browse the repository at this point in the history
In the Telegram API v12 (which is, at time of writing, the latest available for the Raspberry Pi 3), context
based callbacks got upgraded:

   https://github.com/python-telegram-bot/python-telegram-bot/wiki/Transition-guide-to-Version-12.0

Furthermore, the metafile writer got updated to write the parameters as strings instead of bytes (i.e. "w" instead of "wb").
  • Loading branch information
ErwinP committed May 11, 2020
1 parent b6e45f8 commit 5d365a6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*.pyc
.spyderproject
.directory
src/config.py.in
src/config.py
src/pics/*jpg
src/chatlist.ini
src/pics/*.*

21 changes: 11 additions & 10 deletions src/FK_ChatBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,27 @@

# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update):
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hallo, geef het wachtwoord in met commando \n/secret XXXXX\n (XXXX vervang je door het wachtwoord)')


def help(bot, update):
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('Help!')


def echo(bot, update):
def echo(update, context):
"""Echo the user message."""
print ('tekst')
sys.stdout.flush()
update.message.reply_text(update.message.text)

def secret(bot, update, args):
def secret(update, context):
"""Give the secret to accept messages to this bot """
global APPROVED_CHATS
password = "".join(args)
print("\nARGUMENTS: {}".format(context.args))
password = "".join(context.args)
print ('Received pass', password)
sys.stdout.flush()
if password == PASSWORD:
Expand All @@ -96,17 +97,17 @@ def secret(bot, update, args):
config.add_section("Approved")
config.set("Approved", "chatids",
','.join([str(x) for x in APPROVED_CHATS]))
with open(ACCEPTED_CHATS, 'wb') as metafile:
with open(ACCEPTED_CHATS, 'w') as metafile:
config.write(metafile)
else:
update.message.reply_text("Fout wachtwoord!")

def not_authorized(bot, update):
def not_authorized(update, context):
update.message.reply_text('Je bent nog niet gekend, geef eerst het wachtwoord in met commando \n/secret XXXXX\n (XXXX vervang je door het wachtwoord)')

def error(bot, update, error):
def error(bot, update):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, error)
logger.warning('Update "%s" caused error "%s"', update, update.error)


class FilterApprovedChat(BaseFilter):
Expand All @@ -131,7 +132,7 @@ def main():
APPROVED_CHATS = [int(x) for x in chat_ids if x]

# Create the EventHandler and pass it your bot's token.
updater = Updater(TOKEN)
updater = Updater(TOKEN, use_context=True)

# Get the dispatcher to register handlers
dp = updater.dispatcher
Expand Down
2 changes: 2 additions & 0 deletions src/FK_TVbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
tkinter = Tkinter #I decided to use a library reference to avoid potential naming conflicts with people's programs.
else:
import tkinter
Tkinter = tkinter

import threading

Expand All @@ -32,6 +33,7 @@
configparser = ConfigParser
else:
import configparser
ConfigParser = configparser

from PIL import Image, ImageTk
import functools
Expand Down
5 changes: 3 additions & 2 deletions src/picturehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
configparser = ConfigParser
else:
import configparser
ConfigParser = configparser

from telegram.ext import MessageHandler
from telegram.ext import CommandHandler
Expand Down Expand Up @@ -124,7 +125,7 @@ def download(self):
meta.set('message', 'chat_title', '')

# Writing our configuration file
with open(meta_info_filename, 'wb') as metafile:
with open(meta_info_filename, 'w') as metafile:
meta.write(metafile)

def send(self):
Expand Down Expand Up @@ -163,7 +164,7 @@ def get_quality_level(args):
return quality_level

@run_async
def on_photo_received(bot, update):
def on_photo_received(update, args):
logger.info("photo received")

quality_level = 80 # 100 is full best quality, 1 is worst
Expand Down
3 changes: 2 additions & 1 deletion src/replyhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
configparser = ConfigParser
else:
import configparser
ConfigParser = configparser

BASE_FILE_PATH = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])),
'pics', 'reply')
Expand All @@ -35,7 +36,7 @@ def listreplies():
list_of_files = sorted( glob.iglob(REPLIES), key=os.path.getctime, reverse=False)
return list_of_files

def do_reply(bot, job):
def do_reply(job):
"""The reply job. This runs every 120 sec and checks if a reply should
be posted on a picture
Added to the jobqueue when chatbot starts
Expand Down
5 changes: 3 additions & 2 deletions src/videohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
configparser = ConfigParser
else:
import configparser
ConfigParser = configparser

from telegram.ext import MessageHandler
from telegram.ext import CommandHandler
Expand Down Expand Up @@ -79,7 +80,7 @@ def download(self):
meta.set('message', 'chat_title', '')

# Writing our configuration file
with open(meta_info_filename, 'wb') as metafile:
with open(meta_info_filename, 'w') as metafile:
meta.write(metafile)

def send(self):
Expand All @@ -95,7 +96,7 @@ def download_send(self):
#self.remove()

@run_async
def on_video_note_received(bot, update):
def on_video_note_received(update, args):
logger.info("video_note received")

if update and update.message:
Expand Down
5 changes: 3 additions & 2 deletions src/voicehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
configparser = ConfigParser
else:
import configparser
ConfigParser = configparser

from telegram.ext import MessageHandler
from telegram.ext import CommandHandler
Expand Down Expand Up @@ -72,7 +73,7 @@ def download(self):
meta.set('message', 'chat_title', '')

# Writing our configuration file
with open(meta_info_filename, 'wb') as metafile:
with open(meta_info_filename, 'w') as metafile:
meta.write(metafile)

def send(self):
Expand All @@ -88,7 +89,7 @@ def download_send(self):
#self.remove()

@run_async
def on_voice_received(bot, update):
def on_voice_received(update, args):
logger.info("voice received")

if update and update.message:
Expand Down

0 comments on commit 5d365a6

Please sign in to comment.