Lately I have been playing with a great gizmo:

Pimoroni scrollbot project

It’s a fun project with raspberry pi zero w plus a led pHAT and some 3d printed case.

I did interface that to my Golembot, a python Telegram bot that is used to control what messages are scrolled on the led screen.

let me detail what I did to do that.

First of all I suggest to use a gpi hammer header kit to avoid soldering.

Follow pi moroni videos for the setup and it’s quite easy to assemble the kit

Raspbian Setup

Besides the KIT shipped by Pimoroni guys you need an SD card to install the OS on the pi zero.

I bought a 16 gb microsd and used Raspbian as OS. I suggest that because: it’s quite easy, flexible , complete and diffused plus we’re not NOOBS , right?

After installing the image on sd card with rufus , I suggest mounting the /boot partition (it’s FAT so you can do that in windows too)

to fix wifi and enable ethernet over usb, probably you will have to add to config.txt

dtoverlay=dwc2

and in cmdline.txt

modules-load=dwc2,g_ether

after rootwait

Also I had to install on my windows 10 drivers mod-duo-rndis.zip to see ethernet over USB.

This will show up you pi connectd by usb as

You need then the bonjour service on windows to be able to connect to the name raspberrypi.local

Also add an empty ssh file in the same filesystem to enable ssh.

when you can ssh into that ( user pi password raspberry is the default)

you can then setup wifi using raspi-config or by editing /etc/wpa_supplicant.conf

and maybe add a static dhcp reservation on your router for your pi wifi address.

Then you can have fun creating the telegram bot

just ask botfather for a key, then

I chose echobot

as the starting example.

You will need to

pip install telegram pip install scrollphathd

and any other needed module.

My first script was dimmi.py a simple script to scroll stuff on the phat taken from the examples

#!/usr/bin/python # dimmi.py # scrolls what is passed as argument to the script on the led strip import time import scrollphathd as sphd import sys stringa=sys.argv[1] sphd.clear() sphd.rotate(180) sphd.show() sphd.set_brightness(.5) #sphd.write_string('Shiver me timbers!') sphd.write_string(' ' + stringa + ' ') while True: sphd.show() sphd.scroll(1) time.sleep(0.05)

this is my golembot.py

source

#!/usr/bin/env python # -*- coding: utf-8 -*- """Simple Bot to reply to Telegram messages. This program is dedicated to the public domain under the CC0 license. This Bot uses the Updater class to handle the bot. First, a few handler functions are defined. Then, those functions are passed to the Dispatcher and registered at their respective places. Then, the bot is started and runs until we press Ctrl-C on the command line. Usage: Basic Echobot example, repeats messages. Press Ctrl-C on the command line or send a signal to the process to stop the bot. """ from telegram.ext import Updater, CommandHandler, MessageHandler, Filters import logging import os # Enable logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', # level=logging.DEBUG) level=logging.INFO) logger = logging.getLogger(__name__) # secret telegram bot token token ="xxxxxxxxxx" # 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): """Send a message when the command /start is issued.""" update.message.reply_text('Hi!') def quotes(bot, update): """print quotes BTC ETH""" update.message.reply_text("attivo quotes coinbase " + update.message.text) os.system("pkill -9 dimmi.py;/home/pi/quotes/gimmecoinbase.py &") def noquotes(bot, update): """stop quotes BTC ETH""" update.message.reply_text("fermo quotes coinbase ") os.system("ps -ef | grep -v grep | grep coinbase |awk '{ print $2'} | xargs kill -9") def help(bot, update): """Send a message when the command /help is issued.""" update.message.reply_text('I will echo your msg, /quotes prints coinbase quotes /noquotes stops them') def echo(bot, update): """Echo the user message.""" update.message.reply_text("you said " + update.message.text) os.system("pkill -9 dimmi.py;/home/pi/dimmi.py \"" + update.message.text +" \" &") def error(bot, update, error): """Log Errors caused by Updates.""" logger.warning('Update "%s" caused error "%s"', update, error) def main(): """Start the bot.""" # Create the EventHandler and pass it your bot's token. updater = Updater(token) # Get the dispatcher to register handlers dp = updater.dispatcher # on different commands - answer in Telegram dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) dp.add_handler(CommandHandler("quotes", quotes)) dp.add_handler(CommandHandler("noquotes", noquotes)) txtreceived=Filters.text # on noncommand i.e message - echo the message on Telegram dp.add_handler(MessageHandler(txtreceived, echo)) # log all errors dp.add_error_handler(error) # Start the Bot updater.start_polling() # Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle() if __name__ == '__main__': main()

as a bonus I added /quotes command that prints coinbase quotes for BTC and eth

using coinbase libary and the gimmecoinbase.py script

#!/usr/bin/env python # -*- coding: utf-8 -*- # cfr https://github.com/coinbase/coinbase-python # also #https://api.coinbase.com./v2/prices/ETH-EUR/buy import coinbase import json import sys import time import scrollphathd as sphd from scrollphathd.fonts import font5x7 # fix unicode reload(sys) sys.setdefaultencoding('utf8') # client coinbase from coinbase.wallet.client import Client # mia api key client = Client("coinbasekey", "coinbasesecret") # pulisci scroller sphd.clear() # routa scroller sphd.rotate(180) # mostra sphd.show() # luminosita scroller sphd.set_brightness(.5) #sphd.write_string('Shiver me timbers!') # funzione per recuperare ticker da coinbase, ritorna stringa ticker def myticker(): JBTCEUR=client.get_sell_price(currency_pair = 'BTC-EUR') ''' { "amount": "396.40", "base": "ETH", "currency": "EUR" } ''' BTCEUR=JBTCEUR['amount'] JETHEUR=client.get_sell_price(currency_pair = 'ETH-EUR') ETHEUR=JETHEUR['amount'] ticker = "BTC " + BTCEUR + u"\u20AC" + " ETH " + ETHEUR + u"\u20AC" + " " return ticker ticker=myticker() sphd.write_string(' ' + ticker + ' ',font=font5x7) print ticker start_time = time.time() while True: sphd.show() sphd.scroll(1) time.sleep(0.05) elapsed_time = time.time() - start_time if elapsed_time > 120: print "refresh dopo 120 secondi" start_time = time.time() ticker=myticker() print ticker sphd.clear() sphd.write_string(' ' + ticker + ' ',font=font5x7)

sorry bout some italian comments, will translate them asap

this is my systemd unit script

root@raspberrypi:/lib/systemd/system# more golembot.service [Unit] Description=GolemBot Service [Service] Type=simple ExecStart=/usr/bin/python /home/pi/golembot.py > /home/pi/golembot.log 2>&1 User=pi WorkingDirectory=/home/pi [Install] WantedBy=multi-user.target

then you can

systemctl enable golembot systemctl start golembot

What do you think?

Share this: Twitter

Facebook

Like this: Like Loading...

Tags: bot, python, raspberrypi, raspbian, telegram