-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartupTask.cs
More file actions
85 lines (64 loc) · 2.25 KB
/
StartupTask.cs
File metadata and controls
85 lines (64 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Windows.ApplicationModel.Background;
using Windows.Devices.Gpio;
namespace Telegram.Bot.Examples.IoT
{
public sealed class StartupTask : IBackgroundTask
{
private BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
RunBot().Wait();
_deferral.Complete();
}
private static async Task RunBot()
{
InitGPIO(47);
var Bot = new Api("Your API Key");
var offset = 0;
while (true)
{
var updates = await Bot.GetUpdates(offset);
foreach (var update in updates)
{
switch (update.Type)
{
case UpdateType.MessageUpdate:
var message = update.Message;
switch (message.Type)
{
case MessageType.TextMessage:
if (message.Text == "/toggle")
{
ToggleLED();
break;
}
await Bot.SendTextMessage(message.Chat.Id, message.Text,
replyToMessageId: message.MessageId);
break;
}
break;
}
offset = update.Id + 1;
}
}
}
private static GpioPin LED;
private static bool LEDOn;
private static void InitGPIO(int pinNumber)
{
var gpio = GpioController.GetDefault();
if (gpio == null) return;
LED = gpio.OpenPin(pinNumber);
LED.Write(GpioPinValue.Low);
LED.SetDriveMode(GpioPinDriveMode.Output);
}
private static void ToggleLED()
{
LEDOn = !LEDOn;
LED?.Write(LEDOn ? GpioPinValue.High : GpioPinValue.Low);
}
}
}