How to upload a file from slack using SlackClient?

Slack provides an official python client module which you can easily work with.

It’s simple and easy to use. For example, sending a text line to a specific channel is simple enough to call through just one API – api_call(chat.postMessage, channel=channel, text=msg, attachments=attachment, as_user=True)

You can also upload a file to a channel. However, when I was searching some info how to achieve that at the first time, the answer described in the SlackClient’s Github issue was actually wrong. So I tested and added a snippet. Please check the issue page.

Anyway, I also shared the snippet at the public gist. The ‘upload_file’ method takes a long text as the ‘content’ param and upload it as a file. You’ll also upload a file directory from disk or memory if you modify it slightly.

Thanks,

Heejune

Running a CCTV Telegram Bot on your Pi

            

Telepot is a python based telegram bot framework which is lightweight, easy to use and supports all of the up-to-date telegram features. For example, I was able to write a small telegram bot running on Pi2 which takes and sends a photo with only few lines using it. Here is how.

1) Although Telepot supports python 2.7 without async feature, I wanted to use Python 3.x. However, the latest Telepot supports async requires Python 3.5 so I had to download and build Python 3.5 on my Pi2. I followed the instruction introduced here https://sowingseasons.com/blog/software/2016/01/building-python-3-4-on-raspberry-pi-2/19864079 except using python source https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

2) Telepot also provides basic samples. You can also find out other API usages through test code. ‘couter/countera’ example is simple but a good start points if you only require receiving telegram message and sending a reply back.

3) Of course you’ll need a Telegram Bot account. There’s a bot named ‘@BotFather’ which does making a bot for you and makes you setting basic info of it. Just initiate a talk to the account and detailed instruction described here: https://core.telegram.org/bots

4) I used the https://github.com/nickoala/telepot/blob/master/examples/chat/countera.py as the starting point. From the source, MessageCounter.on_chat_message will be called responds to the user’s message. If you want to make the CCTV like telegram bot, then just take a picture and send it back within the method.

5) Takes a picture using the picamera module and replies with it. Remove the temporary saved picture after sending it.

6) here is the result: https://gist.github.com/heejune/afd77aeec49e836fa549fc025962ddd8

Writing a python build script for your Visual C++ project

If you’ve decided to write your own python build script for existing Visual Studio projects, then two questions would probably come up at first.
  1. What utility/commands are available for actually building *.sln/*. vcxproj files?
  2. How will you specify the appropriate version for your *.rc version resource file?
Solution for #1. You can download the MSBuild tools for free from the Microsoft download website, and it provides the solution you need. First, download the MS Build tools 2013(or 2015) depending on your Visual Studio project versions.
Solution for #2. You can easily modify/set the version number string on your *.rc resource file by using python regex. Here is the example.

Downloading a file on the fly from Flask+sqlalchemy

Following code snippet shows how to implement downloading a file with flask.

http://flask.pocoo.org/snippets/32/

def index():
    strIO = StringIO.StringIO()
    strIO.write('Hello from Dan Jacob and Stephane Wirtel !')
    strIO.seek(0)
    return send_file(strIO,
                     attachment_filename="testing.txt",
                     as_attachment=True)

In case you need to use the snippet to support downloading a file which is stored as BLOB originally and to convert it as a downloadable file stream on the fly, then you can use io.BytesIO. See the below:

Let’s say exampleModel is the Sqlalchemy model object and blobField is a blob field which stores blob binary data,

buffer = io.BytesIO(exampleModel.blobField)
buffer.seek(0)    // You don’t need this
return send_file(buffer, attachment_filename=filename)

Thanks.

Heejune.