Created
August 23, 2019 09:18
-
-
Save IdrisCytron/1fdff378f03db473bbfa9cef9eb8d139 to your computer and use it in GitHub Desktop.
Send voice message to Telegram using Raspberry Pi.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gpiozero import LED, Buzzer, Button, OutputDevice | |
from time import time, sleep, strftime | |
from datetime import datetime | |
import os | |
import signal | |
import telepot | |
led1 = LED(17) | |
sw1 = Button(21) | |
buzzer = Buzzer(26) | |
def handle(msg): | |
global telegramText | |
global chat_id | |
global telegramMessage | |
chat_id = msg['chat']['id'] | |
telegramText = msg['text'] | |
print('Message received from ' + str(chat_id)) | |
if telegramText == '/start': | |
bot.sendMessage(chat_id, 'Welcome to Idris Bot') | |
else: | |
buzzer.beep(0.1, 0.1, 2) | |
telegramMessage = True | |
def sw1Pressed(): | |
global sw1ButtonPressed | |
sw1ButtonPressed = True | |
def sw1Released(): | |
global newSpeechRecord | |
newSpeechRecord = True | |
led1.off() | |
os.system('pkill arecord') | |
bot = telepot.Bot('YOUR TELEGRAM BOT TOKEN') | |
bot.message_loop(handle) | |
print("Telegram bot is ready") | |
sw1.when_pressed = sw1Pressed | |
sw1.when_released = sw1Released | |
telegramMessage = False | |
sendAudioEnable = False | |
newSpeechRecord = False | |
sw1ButtonPressed = False | |
buzzer.beep(0.1, 0.1, 1) | |
try: | |
while True: | |
if telegramMessage == True: | |
telegramMessage = False | |
if telegramText == 'ENABLE': | |
sendAudioEnable = True | |
print("Send speech audio is enable.") | |
bot.sendMessage(chat_id, "Send speech audio is enable.") | |
if sw1ButtonPressed == True: | |
sw1ButtonPressed = False | |
led1.on() | |
print("Start recording...") | |
os.system('arecord -D hw:1,0 -f cd record.wav -c 1') | |
if newSpeechRecord == True and sendAudioEnable == True: | |
newSpeechRecord = False | |
print("Send audio to Telegram.") | |
bot.sendAudio(chat_id, audio=open('record.wav', 'rb')) | |
except KeyboardInterrupt: | |
led1.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment