Send Voice Message to Telegram Using Raspberry Pi

Telegram text message is quite common, let's try voice message using Raspberry Pi.

Introduction

There have few Raspberry Pi Telegram tutorials that I have shared before that involve text message such as:

  1. Displaying Telegram Message on Dot Matrix Using Raspberry Pi
  2. Controlling Electrical Appliances Through Telegram Message Using Raspberry Pi

So now I would like to try something new which is voice message. Before that, I recommend you to try the tutorial above as a start for Raspberry Pi Telegram bot application.

Video

This video shows how to send voice message to Telegram using Raspberry Pi.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is python 3 sample program to record voice and send to Telegram. Please edit code at line 39 with your Telegram bot token.

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()

Thank You

References:

  1. Using Telegram Bot with Raspberry Pi: Sharing Text and Files
  2. To use USB mini microphone on Raspbian

Thanks for reading this tutorial. If you have any technical inquiry, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Automatic Dustbin Using REKA:BIT
Ultrasonic HC-SR04P Using Raspberry Pi Pico
Object Sense With Servo Using Maker Pi RP2040
Build Otto DIY Robot Using Maker Pi RP2040
Raspberry Pi RP2040 vs STM32F1 vs SAMD21G18 vs ESP32-S2
Tutorials of Cytron Technologies Scroll to Top