按下按钮时从Raspberry Pi发送电报

时间:2016-02-16 07:50:45

标签: python raspberry-pi gsm telegram

我正在做一个在Raspberry Pi上使用Telegram的项目。目前,我按下按钮时使用XBEE读取高输入。目前,我可以在按下按钮时发送短信。我打算在我的代码中实现电报,这样它就会发送短信和电报信息。我还在我的RPI上安装了Telegram API。如何包含启动电报服务器和发送消息的代码。

我的代码在下面,

if stat1.strip() in "Enable enable ENABLE":

            try:
                    recipient= ( )
                    for item in numbers:
                            recipient= item[0]
                            print recipient
                            gsm.send_sms(recipient, 'Panic activated!')
                            sendSmsNow = True
                            print "panic message sent"
                            time.sleep(5)

            except Exception as e:
                    print e

1 个答案:

答案 0 :(得分:0)

您可以使用python-telegram-bot将消息从Python发送到Telegram。项目的examples文件夹中有一些示例。

它的要点:

from telegram import Updater
import logging

# Enable logging
logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        level=logging.INFO)

logger = logging.getLogger(__name__)


# 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):
    bot.sendMessage(update.message.chat_id, text='Hi!')

查看代码here